没能捕捉到每一个SMS状态(Not able to catch each sms status)

2019-09-02 01:30发布

我试着捕捉到每一个SMS传送的状态,但我的代码是没有得到它,而是只有一个消息的状态。

任何人都可以给提示吗? 这对我的代码。这是工作,但我需要捕捉到每一个短信的每一个状态。

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

Answer 1:

这是现在解决了我修改了我的意图挂起后。 要求代码必须是唯一的,也是我加入这个PendingIntent.FLAG_CANCEL_CURRENT取消当前的意图,或者你可以更新当前的意图。

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


文章来源: Not able to catch each sms status