Not able to catch each sms status

a 夏天 提交于 2019-12-06 11:07:51

问题


Im trying to catch the status of each sms delivery but my code is not getting it but instead the status of only one message.

Anyone can give hints? This si my code.. This is working but I need to catch each status of each 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);} 
}

回答1:


This is now solved after I modified my pending intent. Request code must be unique and also i added this PendingIntent.FLAG_CANCEL_CURRENT to cancel the current intent or you can update current intent.

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


来源:https://stackoverflow.com/questions/16388236/not-able-to-catch-each-sms-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!