94

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.
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
Shouvik
  • 11,350
  • 16
  • 58
  • 89
  • 6
    08/Feb/2019, for anyone searching sticky broadcast and ended here, from [official doc](https://developer.android.com/reference/android/content/Context#removeStickyBroadcast(android.content.Intent)) ```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.``` – fangzhzh Feb 08 '19 at 02:43

4 Answers4

114

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

AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • Hi, I am getting confused with sticky broadcast with the statically registering of the broadcast. I just read somewhere that the difference between registering a broadcast in the manifest file and registering programatically is only that the further one do not unregister the broadcast but it stays there, while the later one unregisters the broadcast in onPause() method. – Shaista Naaz Apr 25 '11 at 06:05
  • Note: in most cases, sticky broadcasts should be avoided. See [the link in the answer from @Nikhil_Katre](http://groups.google.com/group/android-developers/browse_thread/thread/f37e3549a5e1be66?pli=1) for more info – gMale Jul 05 '14 at 20:02
  • @Shaista: manifest receivers operate even when your app is dormant whereas a programmatic receiver only responds when the application it is registered within is running – gMale Jul 05 '14 at 20:07
  • @gmale So, if we are activity and we are in onPause() and we have called unregisterReceiver().. Will we get the broadcast when we resume in onResume() – Kushal Jun 13 '15 at 16:25
  • 1
    @PaulBurke I didn't find name **Mark Murphy** in the above given link. This Link redirects me to the question not on the exact answer. Can you please update the URL?. – Vishal Chhodwani Mar 16 '18 at 10:49
13

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.

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Narendra Motwani
  • 1,085
  • 10
  • 20
9

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.

stkent
  • 19,772
  • 14
  • 85
  • 111
Lou Morda
  • 5,078
  • 2
  • 44
  • 49
3

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

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Arul Pandian
  • 1,685
  • 15
  • 20
  • 2
    I have added the required disclosure of your authorship of the blog you are linking to. You *must* do this yourself from now on, or your posts are subject to being deleted as spam. – Andrew Barber Oct 30 '12 at 13:56