android PendingIntent:有用的requestCode参数
来源:互联网 发布:装修效果图 设计软件 编辑:程序博客网 时间:2024/06/05 00:36
今天真算是吃尽了苦头,往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)
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
FLAG_ONE_SHOT
, FLAG_NO_CREATE
, FLAG_CANCEL_CURRENT
, FLAG_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_CREATE
has 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
- android PendingIntent:有用的requestCode参数
- android 多通知 参数问题, PendingIntent:有用的requestCode参数
- android 多通知 参数问题, PendingIntent:有用的requestCode参数
- PendingIntent requestCode
- Android 通知栏 PendingIntent.getActivity(context, requestCode, intent, flags);参数详解
- Android PendingIntent的getAcetivity()参数的困惑
- android PendingIntent参数详细解析
- android PendingIntent参数详细解析
- Android startActivityForResult(intent, requestCode)的用法。
- Android startActivityForResult(intent, requestCode)的用法。
- Android startActivityForResult(intent, requestCode)的用法。
- (转)Android startActivityForResult(intent, requestCode)的用法
- PendingIntent android.app.PendingIntent.getActivity的使用
- PendingIntent 传递参数的问题
- PendingIntent 传递参数的问题
- Android PendingIntent的使用
- Android PendingIntent的理解
- Android消息通知PendingIntent传递参数出问题
- The Space For App Developers
- RedHat yum 添加sohu源
- tomcat-Java加上ContextPath后Session失效 解决办法
- 粗浅理解RSA
- PLSQL Developer实现数据库间表结构和数据对比和同步
- android PendingIntent:有用的requestCode参数
- 计算机是如何解析我们的汉字和英文?什么是字符集?为什么会出现乱码?
- extJs xtype
- xcode相关的一些知识(一)
- HTML5经典小游戏之坦克(二)
- IOS——Core Data
- 02_vim_基础
- UINavigationController 改变返回按钮的标题
- 高级Bash脚本编程指南(23):复杂命令