Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Sticky Broadcast?

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. 
like image 801
Shouvik Avatar asked Aug 16 '10 05:08

Shouvik


People also ask

What is sticky broadcast used for?

A Sticky Broadcast is a Broadcast that stays around following the moment it is announced to the system. Most Broadcasts are sent, processed within the system and become quickly inaccessible. However, Sticky Broadcasts announce information that remains accessible beyond the point at which they are processed.

What is sticky broadcast permission?

A sticky broadcast is a tool Android developers use for communicating between apps. These broadcasts happen without the user being notified. The Android OS normally treats each application as if it were a separate user.

How do I get rid of sticky broadcasts?

Sticky broadcasts stick around even after your app is gone. The only reliable way to get rid of them is through phone restart.

What is a sticky intent in Android?

Sticky Intent is also a type of Intent which allows communication between a function and a service sendStickyBroadcast(), performs a sendBroadcast(Intent) known as sticky, 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 ...


2 Answers

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.

like image 81
Paul Burke Avatar answered Sep 28 '22 08:09

Paul Burke


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.

like image 34
Narendra Motwani Avatar answered Sep 28 '22 08:09

Narendra Motwani