What Is a Sticky Broadcast

What is a Sticky Broadcast?

If an Activity calls onPause with a normal broadcast, receiving the Broadcast can be missed. A sticky broadcast can be checked after it was initiated in onResume.

Update 6/23/2020

Sticky broadcasts are deprecated.

See sendStickyBroadcast documentation.

This method was deprecated in API level 21.

Sticky broadcasts should not be used. They provide no security (anyone can access them), no protection (anyone can modify them), and many other problems. The recommended pattern is to use a non-sticky broadcast to report that something has changed, with another mechanism for apps to retrieve the current value whenever desired.

Implement

Intent intent = new Intent("some.custom.action");
intent.putExtra("some_boolean", true);
sendStickyBroadcast(intent);

Resources

  • Related post: What is the difference between sendStickyBroadcast and sendBroadcast in Android?

  • See removeStickyBroadcast(Intent), and on API Level 5 +, isInitialStickyBroadcast() for usage in the Receiver's onReceive.

Sticky Broadcast Receivers - how do they work?

Are Broadcast Receivers sticky or Intents sticky?

Broadcast Intents sent via sendStickyBroadcast() are sticky, if you hold the proper permission to send such broadcasts.

Is sendStickyBroadcast(intentA) is the only way to make intentA sticky?

Yes.

If yes, what would happen if I call both sendStickyBroadcast(intentA) and sendBroadcast(intentA) ? Will the intentA be available for future receivers?

AFAIK, the behavior is undocumented. I would not recommend using that pattern.

Why is the onReceive() method executed & the intent returned by registerReceiver() not NULL, since it was not a sendStickyBroadcast() ?

Perhaps you previously sent a sticky broadcast for that same action string. Reboot your device or emulator and try your test again.

Knowing about Sticky intent in Android

Intent - is a message passing mechanism between components of Android, except for Content Provider. You can use Intent to start any
component.

Sticky Intent - Sticks with Android, for future broadcast listeners. For example if BATTERY_LOW event occurs then that Intent
will stick with Android so that any future requests for
BATTERY_LOW, will return the Intent.

Pending Intent - If you want some one to perform any Intent operation at future point of time on behalf of you, then we will use
Pending Intent.

Which Intents/Broadcasts could be sticky in Android?

Where can I find information which Intent/Broadcast can be sticky?

Look at the source code for the Android version of interest, searching for sendStickyBroadcast().

Is exist any restriction or split to sticky / non-sticky or list which Intents work as sticky?

Sticky broadcasts are sent using sendStickyBroadcast(). Technically, it is not tied to a specific Intent.

Sticky broadcasts rebroadcast

does another call to sendStickyBroadcast with the same intent creates a new sticky broadcast or replaces the previous one?

it replaces the current one in case that the Intent is filter equal to the previous one that been already broadcast

I would not use Sticky broadcast because it been deprecated from security reasons from android 5.

also, from my experience - using sticky broadcast for the reason you describes can cause very easy to tons of bugs and unexpected behaviors if you are not considering every case the onReceive() can be called...

Is there a better approach to share a state of some property between all my android classes?

yes. there are much better ways to save state across different application components, depends on the specific use case:

  • save the state as a field (or object) within a singleton class. implementing class as a singleton provide global access to it from any other application component. the singleton state will save as long as your process is alive..

  • in case that you need to save the state and restore it even after the application process stops, you should save the state in one of the persistent ways to do so: SharedPreference is a good choice for saving persistently primitive types like strings, integers..

  • storing the state with Sqlite database whould be the choice for more complex objects and lists..

How can I remove a sticky broadcast from memory?

Sticky broadcasts stick around even after your app is gone. The only reliable way to get rid of them is through phone restart. You can't manually make it go away yourself. This is the intended behavior of a sticky broadcast. It should stick around so that any app that registers itself to receive this broadcast should receive the latest cached version of the broadcast even if the originating app is dead and gone. If you want something that doesn't have this behavior then just use a regular broadcast.

Further reading:

  • Mark Murphy's explanation of sticky broadcasts

  • Sticky broadcasts perils



Related Topics



Leave a reply



Submit