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

From: http://yelinsen.iteye.com/blog/844711

最近小弟在编写一个关于后台数据处理的服务时发现在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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值