Is this possible to check if a broadcast was sent in sticky mode? Can it be aborted/removed?

試著忘記壹切 提交于 2020-01-01 08:21:32

问题


Is this possible to check if a broadcast was sent in sticky mode?

Can we completely abort/remove a sticky broadcast? If yes, then can it be done for both normal and ordered broadcasts?


回答1:


In onReceive() you can use the following calls:

isInitialStickyBroadcast() - This will tell you if the broadcast you are currently processing was sent as "sticky" and was the current one when the BroadcastReceiver was registered.

isOrderedBroadcast() - This will tell you if the broadcast you are currently processing was sent as an "ordered" broadcast.

If you just want to see if there is a "sticky" broadcast, you can call

registerReceiver (BroadcastReceiver receiver, IntentFilter filter)

and supply null as the receiver parameter. This will return any "sticky" broadcast without actually registering the receiver.

You can remove a sticky broadcast using:

removeStickyBroadcast(Intent intent)

However, IMHO that would be counter-productive. Usually "sticky" broadcasts are sent to indicate the current state of something. So removing it would imply that it isn't possible for an application to determine the current state.



来源:https://stackoverflow.com/questions/11824135/is-this-possible-to-check-if-a-broadcast-was-sent-in-sticky-mode-can-it-be-abor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!