未收到Android SMS Manager发送报告和发送报告

时间:2015-03-18 09:43:04

标签: 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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

找到原因,没有注册接收器。添加下面的行,现在像魅力一样工作。

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

答案 1 :(得分:0)

这是一个loooong代码。这一行:

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

本来就够了。

好的,现在我假设您已经调试了应用程序并在接收器中放置了从未被击中的断点。如果是这样,很可能您忘记在清单中包含接收器。

除此之外,我还要检查未决意图中的标志。一些标志会导致传递旧意图。