您的位置:首页 > 其它

Notification内的Intent Extra数据不能更新的问题

2011-11-18 19:56 411 查看
最近小弟在编写一个关于后台数据处理的服务时发现在NotificationManager发送的Notification的PendingIntent内的Intent的Extra数据无法更新,甚至无法获得,测试后发现,相同ID的Notification只能存储第一个Intent的数据,即使使用NotificationManager.cancel也无济于事。

在仔细查看了相关API后发现,使用PendingIntent.getActivity(mContext, 0,mActivity, 0);函数时,将flag置为了0,这时的PendingIntent无法更新,所以发出的Notification打开后,目标Activity无法从getIntent()获取额外的数据,根据API,将getActivity函数的flag值置为PendingIntent.FLAG_UPDATE_CURRENT或者PendingIntent.FLAG_CANCEL_CURRENT即可。

另外放上PendingIntent.getActivity函数可以用到的几个flag值含义:

0:
Default value, if the described PendingIntent already exists, then keep it without change.


FLAG_CANCEL_CURRENT:

Flag for use with
getActivity(Context, int, Intent, int)
,
getBroadcast(Context,
int, Intent, int)
, and
getService(Context, int, Intent, int)
: if the described PendingIntent already exists, the current one is canceled before generating
a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is
not an issue, consider
FLAG_UPDATE_CURRENT
.

Constant Value: 268435456 (0x10000000)


FLAG_NO_CREATE:

Flag for use with
getActivity(Context, int, Intent, int)
,
getBroadcast(Context,
int, Intent, int)
, and
getService(Context, int, Intent, int)
: if the described PendingIntent does not already exist, then simply return null instead of
creating it.


Constant Value: 536870912 (0x20000000)


FLAG_ONE_SHOT:

Flag for use with
getActivity(Context, int, Intent, int)
,
getBroadcast(Context,
int, Intent, int)
, and
getService(Context, int, Intent, int)
: this PendingIntent can only be used once. If set, after
send()
is
called on it, it will be automatically canceled for you and any future attempt to send through it will fail.


Constant Value: 1073741824 (0x40000000)


FLAG_UPDATE_CURRENT:

Flag for use with
getActivity(Context, int, Intent, int)
,
getBroadcast(Context,
int, Intent, int)
, and
getService(Context, int, Intent, int)
: if the described PendingIntent already exists, then keep it but its replace its extra data
with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly
given to it.


Constant Value: 134217728 (0x08000000)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐