android 多通知 参数问题, PendingIntent:有用的requestCode参数

来源:互联网 发布:李天生sql server2008 编辑:程序博客网 时间:2024/06/02 07:30
 

android 多通知 参数问题, PendingIntent:有用的requestCode参数

分类: Android 1450人阅读 评论(0) 收藏 举报
PendingIntentrequestCode

目录(?)[+]

转http://harvey8819.blog.163.com/blog/static/162365181201132691559986/

今天真算是吃尽了苦头,往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参数设成了与noti

0 0