无法捕获每个短信状态

时间:2013-05-05 19:23:18

标签: android sms broadcastreceiver

我试图捕捉每个短信发送的状态,但我的代码没有得到它,而是只有一条消息的状态。

任何人都可以给出提示吗? 这是我的代码..这是有效的,但我需要捕捉每个短信的每个状态。

public void sendMultipart(String msgbody,String msg_receipients,Intent intent)
{
    BroadcastReceiver smsStatusReceiver = new BroadcastReceiver() 
    {   
        @Override
        public void onReceive(Context context, Intent intent)
        {
            context.unregisterReceiver(this); //unregister receiver     
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Logger.logtoFile(tag,"RESULT_OK "+intent.getStringExtra("num"),1);
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Logger.logtoFile(tag,"RESULT_ERROR_GENERIC_FAILURE "+intent.getStringExtra("num"),1);
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Logger.logtoFile(tag,"RESULT_ERROR_NO_SERVICE "+intent.getStringExtra("num"),1);
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Logger.logtoFile(tag,"RESULT_ERROR_RADIO_OFF "+intent.getStringExtra("num"),1);
                    break;
            }
    }
};

IntentFilter intentFilter = new IntentFilter("sent");
MyApplication.getAppContext().registerReceiver(smsStatusReceiver, intentFilter);
Intent intent1 = new Intent("sent");
intent1.putExtra("num", msg_receipients);
SmsManager sms = SmsManager.getDefault();

try
{
    ArrayList<String> messages = sms.divideMessage(msgbody); //Divide msg into chunk
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(messages.size());
    for (int j = 0; j < messages.size(); j++)
        sentIntents.add(PendingIntent.getBroadcast(MyApplication.getAppContext(), 0, intent1, 0));
    sms.sendMultipartTextMessage(msg_receipients, null, messages, sentIntents, null);
    }catch(IllegalArgumentException e)
    {Logger.logtoFile(tag, "SMS sender error  "+e.toString(), 2);}
catch(Exception l)
{Logger.logtoFile(tag, l.toString(), 2);} 
}

1 个答案:

答案 0 :(得分:0)

现在我修改了我的待定意图后解决了这个问题。请求代码必须是唯一的,我还添加了此PendingIntent.FLAG_CANCEL_CURRENT来取消当前意图,或者您可以更新当前意图。

PendingIntent sentPI = PendingIntent.getBroadcast(MyApplication.getAppContext(),smsID++,intentExtra, PendingIntent.FLAG_CANCEL_CURRENT);