What is a Sticky Broadcast?

AndroidAndroid Intent

Android Problem Overview


I came across this term in the android documentation with the accompanying definition

> These are broadcasts whose data is held by the system after being finished, so that clients can quickly retrieve that data without having to wait for the next broadcast.

What does it mean? Can someone elaborate its use with a particular example? I believe we have to request a permission for using this intent? Why so?

<uses-permission android:name="android.permission.BROADCAST_STICKY"/> - Allows an application to broadcast sticky intents.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

sendStickyBroadcast() performs a sendBroadcast(Intent) known as sticky, i.e. the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent). One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver() for that action -- even with a null BroadcastReceiver -- you get the Intent that was last broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.

Solution 3 - Android

The value of a sticky broadcast is the value that was last broadcast and is currently held in the sticky cache. This is not the value of a broadcast that was received right now. I suppose you can say it is like a browser cookie that you can access at any time. The sticky broadcast is now deprecated, per the docs for sticky broadcast methods (e.g.):

> 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.

Solution 4 - Android

A normal broadcast Intent is not available anymore after is was send and processed by the system. If you use the sendStickyBroadcast(Intent) method, the Intent is sticky, meaning the Intent you are sending stays around after the broadcast is complete.

you refer to my blog:enter link description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShouvikView Question on Stackoverflow
Solution 1 - AndroidPaul BurkeView Answer on Stackoverflow
Solution 2 - AndroidNarendra MotwaniView Answer on Stackoverflow
Solution 3 - AndroidLou MordaView Answer on Stackoverflow
Solution 4 - AndroidArul PandianView Answer on Stackoverflow