当前位置: 首页 > web >正文

android PendingIntent:有用的requestCode参数

今天真算是吃尽了苦头,往Android的notifications里增加一条记录的时候,如果只需要一条通知无问题,但如果根据不同情况添加不同的通知,我们知道可通过方法notificationManager.notify(id,notification)的id参数指定不同的id就行了,但事实上通常会出现以下问题:我们一般会通过一个PendingIntent指定一个Intent,使点击notification后跳转到指定的视图,可是奇怪的是,当我添加了两条以上通知并通过Bundle传送了一些数据之后,后面传的Bundle中的数据会把前面的覆盖掉,也就是说无论添加了多少条通知,点击跳转过去的视图都变成一样的了。百思不得其解。

搜了大量资料后,才偶然发现是PendingIntent 的问题:

PendingIntent 需要通过以下方法取得:PendingIntent.getActivity (Context context, int requestCode, Intent intent, int flags),看sdk,对这个方法这样描述:

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
Since:  API Level 1

Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

Parameters
contextThe Context in which this PendingIntent should start the activity.
requestCodePrivate request code for the sender (currently not used).
intentIntent of the activity to be launched.
flagsMay be FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
Returns
  • Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATEhas been supplied.

 

我们看到requestCode打了括号说明是currently not used,既然还没使用,我就像网上很多例子一样随便给一个0就算了。可实际上这个参数是有用的:当同时有多个通知的时候,可通过这个参数来互相区别,否则的话,就像我开始遇到的情况,后加进去的Intent会把前面的盖掉。弄清楚了这点,事情就好办了,我把PendingIntent.getActivity的requestCode参数设成了与notificationManager.notify(id,notification)的id一致,这样就可保证每个notification对就一个唯一的Intent。修改后编译运行,一切正常!!!

参考文章:http://www.cnmsdn.com/html/201006/1277172820ID6307.html

http://www.xdnf.cn/news/11703.html

相关文章:

  • 如何把大容量10G的文件分享给别人?整理了3个简单的方法~
  • 三级数据库技术考点(详解!!),35岁技术人如何转型做管理
  • python爬取某站磁力链
  • uniapp的那些坑
  • 公司面试题
  • 主打实用!分享两个制作答辩PPT的技巧,还有8套高校通用毕业论文PPT模板
  • 高速动车组全谱系图解
  • 计算机视觉简介
  • Android应用程序 --- WakeLock 保持后台唤醒状态
  • 微信小程序详细教程(建议收藏)
  • 使用PHPProxy把虚拟空间变成HTTP代理(ZT)
  • Android Preference 须知
  • DSP的入门学习(一)
  • 结构(4)通讯录实战
  • 李开复写给中国大学生的第四封信
  • 怎么看电脑配置?3个简易指南告诉你
  • 必须安装三星系列android系统智能手机usb驱动程序,三星安卓4.0怎么刷机 三星安卓4.0刷机教程...
  • Firework使用简单介绍
  • webservice安全_Web 安全测试之信息泄漏测试
  • 无须插件!用WIN$Man安装win7更轻松【xpghost】
  • Symbian S60平台UCWEB6.3 Beta版发布啦
  • myEclipse 工具 优化 和 初始化设置
  • Intel, AMD及VIA CPU的微架构(16)
  • 《GreenPlum系列-开发相关》GreenPlum数据库及语法使用
  • js中对象方法hasOwnProperty()讲解
  • 线程池ThreadPoolExecutor 的使用
  • 排序算法总结——归并排序
  • 信号强度(RSSI)知识整理
  • AVPlayer 播放在线视频和本地文件的设置区别
  • Hydra(九头蛇)工具使用(非常详细)零基础入门到精通,收藏这一篇就够了!!!