`
kanwoerzi
  • 浏览: 1648328 次
文章分类
社区版块
存档分类
最新评论

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

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

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

相关推荐

    google_AIP 方法解释

    requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent) 通过给定的Provider名称,周期性地通知当前Activity requestLocationUpdates(String provider, long minTime, ...

    Android中AlarmManager+Notification实现定时通知提醒功能

    AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver)。本文将讲解一下如何使用AlarmManager实现定时提醒...

    android 本机定时推送通知

    PendingIntent pi= PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(this,MyReceiver.class), 0); if(b){ // just use current time + 10s as the Alarm time. Calendar c=Calendar....

    Android中通知Notification使用实例(振动、灯光、声音)

    本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android....

    BroadcastTileSupport:一个简单的实现,展示了如何在Android M的System UI Tuner中使用新的“广播图块”

    如果尚未完成操作,请在通知栏中按住“设置”图标约8秒钟。 图标应更改为在齿轮旁边显示扳手。 然后,您会在“设置”末尾看到“系统UI调谐器”选项。 通过系统UI调谐器添加广播图块。 如果您使用的是此存储库中...

    android中创建通知栏Notification代码实例

    ///// 第一步:获取NotificationManager NotificationManager nm = (NotificationManager) getSystemService... //PendingIntent是待执行的Intent PendingIntent pi = PendingIntent.getActivity(this, 0

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量...

    android 设置闹钟及通知示例

    简单说一下这次demo内容,首先做一个设置一次性闹钟,先得到alarmManager,打开一个时间对话框,在里面设置闹钟的时间,时间一到发送广播,然后广播接受者接到跳转到新的activity播放音乐。接着是一个反复闹钟,最后...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量...

    精通ANDROID 3(中文版)1/2

    5.2 Android中可用的Intent  5.3 Intent的组成  5.3.1 Intent和数据URI  5.3.2 一般操作  5.3.3 使用extra信息  5.3.4 使用组件直接调用活动  5.3.5 Intent类别  5.3.6 将Intent解析为组件的规则  ...

    精通Android 3 (中文版)2/2

    5.2 Android中可用的Intent  5.3 Intent的组成  5.3.1 Intent和数据URI  5.3.2 一般操作  5.3.3 使用extra信息  5.3.4 使用组件直接调用活动  5.3.5 Intent类别  5.3.6 将Intent解析为组件的规则  ...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少...

    Google Android SDK开发范例大全的目录

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量...

    Google+Android+SDK开发范例大全

    6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——...

    Google Android sdk 开发范例大全 部分章节代码

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量...

    Google Android SDK 开发范例大全01

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量...

    Google Android SDK 开发范例大全02

    6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之onCallStateChanged 6.7 有来电,发送邮件通知——PhoneStateListener与ACTION_SEND 6.8 存储卡剩余多少容量...

Global site tag (gtag.js) - Google Analytics