2

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

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

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);} 
}
4

1 回答 1

0

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

PendingIntent sentPI = PendingIntent.getBroadcast(MyApplication.getAppContext(),smsID++,intentExtra, PendingIntent.FLAG_CANCEL_CURRENT);
于 2013-05-18T06:51:20.517 回答