android 多次调用PendingIntent.getBroadcast intent数据不更新问题
来源:互联网 发布:js微信通讯录字母索引 编辑:程序博客网 时间:2024/05/16 06:13
实验环境:
windows 7
Android Developer Tools
Build: v21.1.0-569685
今日在开发一个小app的时候遇到一个问题:
该app需要实现本机发送短信,遂,通过
SmsManager + PendingIntent.getBroadcast 的方式实现,因为短信队列里包含多个号码,且发送完毕后需要更新发送状态,所以需要在BroadcastReceiver回调中得到发送短信的ID,大体实现如下(大家也可以参考下具体发送短信的机制):
1、接收短信发送后的回调函数:
private SmsManager sms;private BroadcastReceiver sendBroadcastReceiver;private BroadcastReceiver deliveryBroadcastReceiver;public void initSmsBroadcast() { sms = SmsManager.getDefault(); sendBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { Bundle b = _intent.getExtras(); Object[] lstName = b.keySet().toArray(); String phone_number = ""; String sms_id = ""; for(int i = 0; i < lstName.length; i++) { String keyName=lstName[i].toString(); if (keyName.equals("phone")) { phone_number = b.get(keyName).toString(); } if (keyName.equals("sms_id")) { sms_id = b.get(keyName).toString(); } } switch (getResultCode()) { case Activity.RESULT_OK: receiveSmsLog("SEND", "to:" + phone_number + "- SMS sent success actions"); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: receiveSmsLog("SEND", "to:" + phone_number + "- SMS generic failure actions"); break; case SmsManager.RESULT_ERROR_RADIO_OFF: receiveSmsLog("SEND", "to:" + phone_number + "- SMS radio off failure actions"); break; case SmsManager.RESULT_ERROR_NULL_PDU: receiveSmsLog("SEND", "to:" + phone_number + "- SMS null PDU failure actions"); break; } } }; registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT_SMS_ACTION)); deliveryBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { Bundle b = _intent.getExtras(); Object[] lstName = b.keySet().toArray(); String phone_number = ""; String sms_id = ""; for(int i = 0; i < lstName.length; i++) { String keyName=lstName[i].toString(); if (keyName.equals("phone")) { phone_number = b.get(keyName).toString(); } if (keyName.equals("sms_id")) { sms_id = b.get(keyName).toString(); } } Log.d("sms_id", sms_id + ""); receiveSmsLog("DELIVER", "to:" + phone_number + "- SMS delivered actions"); } }; registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED_SMS_ACTION)); }
2、开始发送短信:
private int pend_times = 0;public void sendSms(final int sms_id, final String phone_number, String msg_str) { Intent sentIntent = new Intent(SENT_SMS_ACTION); sentIntent.putExtra("sms_id", sms_id); sentIntent.putExtra("phone", phone_number); Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION); deliverIntent.putExtra("sms_id", sms_id); deliverIntent.putExtra("phone", phone_number); pend_times++; PendingIntent sentPI = PendingIntent.getBroadcast(this, pend_times, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent deliverPI = PendingIntent.getBroadcast(this, pend_times, deliverIntent, PendingIntent.FLAG_UPDATE_CURRENT); if (msg_str.length() > 70) { ArrayList<String> msgs = sms.divideMessage(msg_str); for (String msg : msgs) { sms.sendTextMessage(phone_number, null, msg, sentPI, deliverPI); } } else { sms.sendTextMessage(phone_number, null, msg_str, sentPI, deliverPI); } }
开始的时候多次发送短信成功,但是intent中的数据总是第一次传入的数据,其后就不能更新,通过google多次,尽一天的时间得到的结论是:
需要在PendingIntent.getBroadcast函数中设置第4个参数为 PendingIntent.FLAG_UPDATE_CURRENT
即:PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
但是我这里还是不能成功,百思不得其解啊。
于是就修改了第二个参数requestCode,即
private int pend_times = 0; pend_times++;PendingIntent sentPI = PendingIntent.getBroadcast(this, pend_times, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
测试通过,哈哈~~。
有的同学还提出修改了requestCode也没起作用,我建议同时修改
PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)的requestCode 和flags试试,好运~~
- android 多次调用PendingIntent.getBroadcast intent数据不更新问题
- android 多次调用PendingIntent.getBroadcast intent数据不更新问题
- android中多次调用PendingIntent.getBroadcast(),intent数据不更新问题
- Android PendingIntent.getBroadcast intent数据不更新问题
- 手机高温警告提示内容不匹配的分析【多次调用PendingIntent.getBroadcast,intent数据不更新】
- android PendingIntent.getBroadcast 之坑
- notification PendingIntent跳转数据不更新问题
- Android 使用 PendingIntent 无法调用Intent
- PendingIntent不同Intent数据的覆盖问题
- Android 通知PendingIntent意图发送广播,但Intent数据无更新? 解决方法
- 巨坑,PendingIntent.getBroadcast
- Android PendingIntent的getActivity()、getBroadcast()、getService()方法的区别
- Android ListView getView多次不规律调用问题
- PendingIntent 传 intent 问题
- PendingIntent 传 intent 问题
- 【Android】pendingIntent和Intent
- Android - Intent、PendingIntent、IntentFilter、BroadcastReceiver
- android Intent PendingIntent的区别
- Hough变换直线检测
- java version : 找出数组中两个只出现一次的数字
- HDU 4870 Rating (概率+高斯消元)
- hibernate学习之——many-to-many(双向多对多关系)
- Spring 注解学习手札(一) 构建简单Web应用
- android 多次调用PendingIntent.getBroadcast intent数据不更新问题
- sed
- HTML-表格基础
- poj1182~食物链~种类并查集
- 用 Maven 做项目管理
- Eclipse中Maven WEB工程tomcat项目添加调试以及项目发布细节记录
- 使用CAShapeLayer与UIBezierPath画出想要的图形 -- 下载进度条
- 微信摇一摇抽奖将改变人们的娱乐方式
- 利用TeamViewer + CCproxy实现翻过公司上网限制