未收到 Android 短信管理器发送报告和已发送报告

标签 android smsmanager

我实现了这样我的服务将运行并从服务器检索 SMS 并在一个线程中发送,但我现在的问题是发送和交付报告没有调用我的 PI。下面是我的代码,哪位高手可以帮我找出我哪里做错了?

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.telephony.SmsManager;
import android.text.format.Time;
import android.widget.Toast;

public class ServiceSMSSender extends Service 
{
    Boolean continueRun = true;
    PowerManager.WakeLock wl;
    WifiLock wifiLock;

    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {          
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
        wl.acquire();

        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "TAG");
        wifiLock.acquire();

        ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
        if(cd.isConnectingToInternet())
        {  
            //get the center list
            WsConnection wsConn = new WsConnection();
            ObjCenter objCenters[] = wsConn.GetCenterList(getApplicationContext());

            if(objCenters != null)
            {
                ((MyGlobal) this.getApplication()).SetCenterList(objCenters);               

                Intent summaryIntent = new Intent(this, SummaryActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(this, 0, summaryIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

                Notification mNotification = new Notification.Builder(this)

                .setContentTitle(getString(R.string.txtServiceSMSSender))
                .setSmallIcon(R.drawable.notification_icon)
                .setContentIntent(pIntent)
                .build();

                mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

                startForeground(9999, mNotification);

                final Handler handler = new Handler()
                {
                    @Override
                    public void handleMessage(Message msg) 
                    {
                        super.handleMessage(msg);

                        ObjCenter[] objCenters = ((MyGlobal) getApplication()).GetCenterList();

                        for(int i = 0; i < objCenters.length; i++)
                        {
                            new ExecuteCenterSMSProcessing(objCenters[i]).execute();
                        }                                           

                        System.gc();
                    }
                };
                new Thread(new Runnable(){
                    public void run() {

                        do
                        {
                            try 
                            {
                                handler.sendEmptyMessage(0);

                                Thread.sleep(5000); //2 minutes                     
                            } 
                            catch (InterruptedException e)
                            {    
                                e.printStackTrace();
                            } 

                        }while(continueRun);
                    }
                }).start(); 

                return Service.START_STICKY;
            }
        }

        stopForeground(true);
        stopSelf();

        return Service.START_NOT_STICKY;           
    }

    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }

    @Override
    public void onDestroy() 
    {       
        continueRun = false;

        stopForeground(true);

        wl.release();
    }

    private class ExecuteCenterSMSProcessing extends AsyncTask<Void, Void, Void>
    { 
        ObjCenter objCenter;

        public ExecuteCenterSMSProcessing(ObjCenter objCenter) 
        {   
            this.objCenter = objCenter;
        }

        protected Void doInBackground(Void... params) 
        {                       
            WsMessagePool wsMessagePool = new WsMessagePool();

            ObjMessagePool[] objMessagePools = wsMessagePool.GetUnsentMessage(getApplicationContext(), objCenter.centerId);

            if(objMessagePools != null)
            {
                for(int i = 0; i < objMessagePools.length; i++)
                {                                       
                    SmsManager smsManager = SmsManager.getDefault();

                    ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
                    ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();

                    Intent intentSent = new Intent(getApplicationContext(), SmsSentReceiver.class);
                    Intent intentDelivered = new Intent(getApplicationContext(), SmsDeliveredReceiver.class);

                    intentSent.putExtra("messagePoolId", objMessagePools[i].messagePoolId);
                    intentDelivered.putExtra("messagePoolId", objMessagePools[i].messagePoolId);

                    intentSent.putExtra("centerId", objCenter.centerId);
                    intentDelivered.putExtra("centerId", objCenter.centerId);

                    PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, intentSent, 0);
                    PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0, intentDelivered, 0);

                    //check and manage Unicode
                    String message = null;
                    if (!MyGlobal.HasUnicode(objMessagePools[i].message))
                    {
                        try
                        {
                            byte[] utf16 = objMessagePools[i].message.getBytes("UTF-16");
                            message = new String(utf16, "UTF-16");
                        }
                        catch (UnsupportedEncodingException ex)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        message = objMessagePools[i].message;
                    }

                    ArrayList<String> msgArray = smsManager.divideMessage(message);

                    for (int j = 0; j < msgArray.size(); j++) 
                    {
                        sentPendingIntents.add(j, sentPI);
                        deliveredPendingIntents.add(j, deliveredPI);
                    }

                    intentSent.putExtra("noOfUnit", msgArray.size());

                    smsManager.sendMultipartTextMessage(objMessagePools[i].recipientMobileNo, null, msgArray, sentPendingIntents, deliveredPendingIntents);                 
                }
            }

            return null;
        }
    }
}

然后我为接收器创建了 2 个类

public class SmsDeliveredReceiver extends BroadcastReceiver 
{
    public final int SUPER_SMS_STATUS_SUCCESS = 2002;
    public final int SUPER_SMS_STATUS_DELIVERY_FAILED = 2007;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        int messagePoolId = intent.getIntExtra("messagePoolId", 0);
        int centerId = intent.getIntExtra("centerId", 0);

        if(messagePoolId != 0)
        {
            WsMessagePool wsMessagePool = new WsMessagePool();

            switch (getResultCode()) 
            {
                case Activity.RESULT_OK:

                    wsMessagePool.UpdateDeliveryReport(context, centerId, messagePoolId, SUPER_SMS_STATUS_SUCCESS);

                    break;

                case Activity.RESULT_CANCELED:

                    wsMessagePool.UpdateDeliveryReport(context, centerId, messagePoolId, SUPER_SMS_STATUS_DELIVERY_FAILED);

                    break;
            }
        }
    }
}

还有

public class SmsSentReceiver extends BroadcastReceiver 
{
    public final int SUPER_SMS_STATUS_SENT = 2001;
    public final int SUPER_SMS_STATUS_GENERIC_ERROR = 2003;
    public final int SUPER_SMS_STATUS_NO_SERVICE = 2004;
    public final int SUPER_SMS_STATUS_NULL_PDU = 2005;
    public final int SUPER_SMS_STATUS_RADIO_OFF = 2006;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        int messagePoolId = intent.getIntExtra("messagePoolId", 0);
        int centerId = intent.getIntExtra("centerId", 0);
        int noOfUnit = intent.getIntExtra("noOfUnit", 0);

        if(messagePoolId != 0)
        {
            WsMessagePool wsMessagePool = new WsMessagePool();

            switch (getResultCode()) 
            {
                case Activity.RESULT_OK:

                    wsMessagePool.UpdateStatus(context, centerId, messagePoolId, true, SUPER_SMS_STATUS_SENT, noOfUnit);

                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                    wsMessagePool.UpdateStatus(context, centerId, messagePoolId, false, SUPER_SMS_STATUS_GENERIC_ERROR, noOfUnit);

                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:

                    wsMessagePool.UpdateStatus(context, centerId, messagePoolId, false, SUPER_SMS_STATUS_NO_SERVICE, noOfUnit);

                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:

                    wsMessagePool.UpdateStatus(context, centerId, messagePoolId, false, SUPER_SMS_STATUS_NULL_PDU, noOfUnit);

                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:

                    wsMessagePool.UpdateStatus(context, centerId, messagePoolId, false, SUPER_SMS_STATUS_RADIO_OFF, noOfUnit);

                    break;
            }
        }
    }
}

最佳答案

找到原因,没有注册receiver。添加了下面一行,现在就像一个魅力一样工作。

getApplicationContext().registerReceiver(new SmsSentReceiver(), new IntentFilter(SMS_SENT));
getApplicationContext().registerReceiver(new SmsDeliveredReceiver(), new IntentFilter(SMS_DELIVERED));

关于未收到 Android 短信管理器发送报告和已发送报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118625/

相关文章:

android - 至少一次在不打开应用程序的情况下接收到 Android 应用程序的短信

android - ActionBar setProgressBarIndeterminateVisibility(false) 不适用于 android 2.3

android - 短信不在android中发送

android - SMSManager 不适用于三星 Galaxy S3 LTE

android - 如何知道从 Android BroadcastReceiver 发送/传递了哪些 Sms?

java - Android - 如何以编程方式使我的应用程序默认短信应用程序

android - Android 手机源代码/驱动程序的开放性

Android无法识别的字符显示为 "?"

android - 模块根文件夹中缺少文件 google-services.json

android - 如何在不使用任何按钮的情况下发送短信