关于Intent.FLAG_RECEIVER_REPLACE_PENDING

本文深入探讨了在Android开发过程中遇到的一个问题:如何在发送广播时使用FLAG_RECEIVER_REPLACE_PENDING标记避免旧广播被新广播覆盖,确保特定Intent消息的正确传递。同时,解释了FLAG_RECEIVER_REGISTERED_ONLY标记对于接收Intent的影响。

/**

* If set, when sending a broadcast the new broadcast will replace

* any existing pending broadcast that matches it. Matching is defined

* by {@link Intent#filterEquals(Intent) Intent.filterEquals} returning

* true for the intents of the two broadcasts. When a match is found,

* the new broadcast (and receivers associated with it) will replace the

* existing one in the pending broadcast list, remaining at the same

* position in the list.

*

* <p>This flag is most typically used with sticky broadcasts, which

* only care about delivering the most recent values of the broadcast

* to their receivers.

*/

public static final int FLAG_RECEIVER_REPLACE_PENDING = 0x20000000;

前段时间在开发的过程中发现了一个很奇怪的问题, 就是在一个BroadcastReceiver里 接收一个Intent 的消息, 但是并不是这个Intent 的所有消息都可以接收得到。

后来查了一下发送Intent 的地方, 才发现这个Intent 加了一个Flag ,FLAG_RECEIVER_REPLACE_PENDING, 这个flag 将会将之前的Intent 替代掉。加了这个flag,在发送一系列的这样的Intent 之后, 中间有些Intent 有可能在你还没有来得及处理的时候, 就被替代掉了。

另外, 还有一个Flag: FLAG_RECEIVER_REGISTERED_ONLY

如果Intent 加了这个Flag, 那么在Androidmanifest.xml 里定义的Receiver 是接收不到这样的Intent 的。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值