发通知 PendingIntent 中Intent 内容没有更新

文章前部分来源: http://byandby.iteye.com/blog/1120375
Xml代码   收藏代码
  1. <activity android:name="SkyfileActivity"   
  2.          android:launchMode="singleTask" />  
   当我们把Activity 启动模式设置为 singleTask 之后 当我们下次 再去 用Intent 启动 这个 Activity 的时候 就不会去调用 onCreate方法 而是去调用onNewIntent()方法 然后把Intent中的数据传给它 , 前几天遇到的问题是  当我 发一个通知给状态栏  然后点击这个通知  自然会执行 PendingIntent 里边的Intent。  但是   在Activity那边的 onNewIntent()方法里边 得到的数据  不是最新的 也就是说 是 第一次的 以后 不管我怎么点通知 它都 是 第一次点击通知得到的数据,当以后再点击通知的时候其实 数据已经变了 但是 onNewIntent()方法总是得不到最新的数据,   无语了很久,  去 农民伯伯翻译组 发问得解    需要给 PendingIntent 加一个 FLAG  

Java代码   收藏代码
  1. PendingIntent contentIntentBegin = PendingIntent.getActivity(  
  2.                     notificationContext, 0, inStart, PendingIntent.FLAG_UPDATE_CURRENT);  


   最后一个参数就是 FLAG,这个FLAG 的 意思就是:如果系统中已存在该PendingIntent对象,那么系统将保留该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。这个非常有用,例如之前提到的,我们需要在每次更新之后更新Intent中的Extras数据,达到在不同时机传递给MainActivity不同的参数,实现不同的效果。  

   就是 Intent里边的数据没更新而已, 很2个问题 搞了很久 才发现原来加个FLAG 就行了,有点伤不起了。!! 发通知 PendingIntent 中Intent 内容没有更新 - hubingforever - 民主与科学  

  代码片段  
Java代码   收藏代码
  1. public void showNotiMessageBegin(String message, int requestCode,  
  2.         String itemid) {  
  3.     notification = new Notification(R.drawable.skyfile_upload_noti,  
  4.             message, System.currentTimeMillis());  
  5.     if (requestCode == 1 && notification.contentIntent == null) {  
  6.         int index = itemid.lastIndexOf("/");  
  7.         final String backPath1 = itemid  
  8.                 .substring(0, index == 0 ? 1 : index);  
  9.                      
  10.         Intent inStart = new Intent(notificationContext, SkyfileActivity.class);  
  11.         inStart.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  12.         inStart.setData(Uri.parse(MetaDataView.class.getName()));  
  13.         inStart.putExtra(MetaDataView.BUNDLE_PATH, backPath1);  
  14.         PendingIntent contentIntentBegin = PendingIntent.getActivity(  
  15.                 notificationContext, 0, inStart, PendingIntent.FLAG_UPDATE_CURRENT);  
  16.         notification.contentIntent = contentIntentBegin;  
  17.         notification.flags |= Notification.FLAG_AUTO_CANCEL;  
  18.         notification.setLatestEventInfo(notificationContext,  
  19.                 UPLOATTITLE, message, contentIntentBegin);  
  20.         notificationMgr.notify(1, notification);  
  21.     } else {  
  22.         notification.contentIntent.cancel();  
  23.     }  
  24. }  
本人另注:
其实如果多注意下PendingIntent的文档,就应该明白这里应该用 PendingIntent.FLAG_UPDATE_CURRENT
如果我在从系统中提取一个 PendingIntent ,而系统中有一个和你描述的 PendingIntent 对等的 PendingInent , 那么系统会直接返回和该 PendingIntent 其实是同一token的 PendingIntent ,而不是一个新的 token PendingIntent 。如果我们使用了 FLAG_UPDATE_CURRENT 的话,新的 Intent 会更新之前 PendingIntent 中的 Intent 对象数据,当然也会更新Intent中的 Extras
关于 PendingIntent的更多内容请参照《 PendingIntent详解
注意两个PendingIntent对等是指它们的operation一样, 且其它们的Intent的action, data, categories, components和flags都一样但是它们的Intent的Extra可以不一样
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值