what is the difference between sendStickyBroadcast and sendBroadcast in Android

AndroidAndroid IntentAndroid Broadcast

Android Problem Overview


What is the difference between sendStickyBroadcast and sendBroadcast in Android?

Android Solutions


Solution 1 - Android

Here is what the Android SDK says about sendStickyBroadcast():

> Perform a sendBroadcast(Intent) that > is "sticky," meaning 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 2 - Android

Types :- Local,Normal,Ordered and Sticky

Normal Broadcast

:- use sendBroadcast()

:- asynchronous broadcast

:- any receiver receives broadcast not any particular order

Ordered Broadcast

:- use sendOrderedBroadcast()

:- synchronous broadcast

:- receiver receives broadcast in priority base

:- we can also simply abort broadcast in this type

Local Broadcast

:- use only when broadcast is used only inside same process

Sticky Broadcast

:- normal broadcast intent is not available any more after this was send and processed by the system.

:- use sendStickyBroadcast(Intent)

:- the corresponding intent is sticky, meaning the intent you are sending stays around after the broadcast is complete.

:- because of this others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter).

:- apart from this same as sendBroadcast(Intent).

Solution 3 - Android

sendbroadcast() - normal broadcast, but we can set priority as well.

sendstickybroadcast() - intent passed with this will be stick for future users who are registering through code (dynamic receivers). The broadcast that will stick with android, and will be re-delivered or re-broadcasted to the future requests from any broadcast receivers

When somebody sends a sticky broadcast using sendstickyBroadcast(intent); then that broadcast will be available for the future users who are using dynamic receivers.

But Now you should not use sendStickyBroadcast() method it is deprecated

From Android 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

I hope this helps.

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
QuestioncobjectView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidUmang KothariView Answer on Stackoverflow
Solution 3 - AndroidRajeshView Answer on Stackoverflow