没能捕捉到每一个SMS状态 [英] Not able to catch each sms status

查看:103
本文介绍了没能捕捉到每一个SMS状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着捕捉到每一个短信发送的状态,但我的code未得到它,而是只有一个消息的状态。

任何人都可以给提示吗?
这州市我的code ..这是工作,但我需要捕捉每条短信的每一个状态。

 公共无效sendMultipart(字符串msgbody,字符串msg_receipients,意图意图)
{
    广播接收器smsStatusReceiver =新的广播接收器()
    {
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图)
        {
            context.unregisterReceiver(本); //注销接收器
            开关(的getResult code())
            {
                案例Activity.RESULT_OK:
                    Logger.logtoFile(标签,RESULT_OK+ intent.getStringExtra(民),1);
                    打破;
                案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Logger.logtoFile(标签,RESULT_ERROR_GENERIC_FAILURE+ intent.getStringExtra(民),1);
                    打破;
                案例SmsManager.RESULT_ERROR_NO_SERVICE:
                    Logger.logtoFile(标签,RESULT_ERROR_NO_SERVICE+ intent.getStringExtra(民),1);
                    打破;
                案例SmsManager.RESULT_ERROR_RADIO_OFF:
                    Logger.logtoFile(标签,RESULT_ERROR_RADIO_OFF+ intent.getStringExtra(民),1);
                    打破;
            }
    }
};IntentFilter的IntentFilter的=新的IntentFilter(发送);
MyApplication.getAppContext()registerReceiver(smsStatusReceiver,IntentFilter的)。
意图int​​ent1 =新意图(发送);
intent1.putExtra(民,msg_receipients);
SmsManager短信= SmsManager.getDefault();尝试
{
    ArrayList的<串GT;消息= sms.divideMessage(msgbody); //分割成味精块
    ArrayList的<&的PendingIntent GT; sentIntents =新的ArrayList<&的PendingIntent GT;(messages.size());
    对于(INT J = 0; J< messages.size(); J ++)
        sentIntents.add(PendingIntent.getBroadcast(MyApplication.getAppContext(),0,intent1,0));
    sms.sendMultipartTextMessage(msg_receipients,空,消息,sentIntents,NULL);
    }赶上(抛出:IllegalArgumentException五)
    {Logger.logtoFile(标签,短信发送错误+ e.toString(),2);}
赶上(例外升)
{Logger.logtoFile(标签,l.toString(),2);}
}


解决方案

这是目前解决后,我修改了我的悬而未决的意图。请求code必须是唯一的,也是我加入这个PendingIntent.FLAG_CANCEL_CURRENT取消当前的意图,也可以更新当前的意图。

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

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

解决方案

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

这篇关于没能捕捉到每一个SMS状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆