android 多次调用PendingIntent.getBroadcast intent数据不更新问题

转自:http://www.reasono.com/?p=318

实验环境:

windows 7

Android Developer Tools

Build: v21.1.0-569685

今日在开发一个小app的时候遇到一个问题:

该app需要实现本机发送短信,遂,通过

SmsManager + PendingIntent.getBroadcast 的方式实现,因为短信队列里包含多个号码,且发送完毕后需要更新发送状态,所以需要在BroadcastReceiver回调中得到发送短信的ID,大体实现如下(大家也可以参考下具体发送短信的机制):

1、接收短信发送后的回调函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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、开始发送短信:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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,即

1
2
3
4
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试试,好运~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值