what is the difference between sendStickyBroadcast and sendBroadcast in Android
Asked Answered
R

3

107

What is the difference between sendStickyBroadcast and sendBroadcast in Android?

Rattat answered 6/4, 2010 at 11:42 Comment(0)
S
121

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.

Surveying answered 6/4, 2010 at 12:22 Comment(17)
Just how sticky is the intent? When you call registerReceiver a second time does it return the same intent again? (Presuming no additional intent with that action.)Luganda
What happens to sticky intents over: power down? screen off? etc.Luganda
@phreed: "When you call registerReceiver a second time does it return the same intent again?" -- yes. "power down?" -- they go away, just as anything in RAM does. "screen off?" -- no effect.Surveying
Just to add that sticky broadcasts are strictly discouraged by Ms Hackborn : groups.google.com/d/msg/android-developers/8341SaXhvmY/…. It is an old post but probably applies stillMidmost
@ Commonsware: i cannot understand your points.Explain with normal intent example and with sticky intent example for same scenario.Then it will usefull to me get a clear ideaNolly
skillgun.com/question/543/android/receivers/…Dalton
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()Hartwell
@Kushal: If you are referring to a sticky broadcast, you should get the last-sent broadcast for that action on any registerReceiver() call. Note, though, that sticky broadcasts are now deprecated.Surveying
@Surveying thank you sir for reply.. So what should be alternative for sticky broadcast now? if they are deprecatedHartwell
@Kushal: "So what should be alternative for sticky broadcast now?" -- I cannot really answer that, as I do not know what your use case is. You might consider asking a new Stack Overflow question, where you describe your business requirement, explain how you were thinking of solving it via sticky broadcasts, and ask for alternative ways of solving that same problem.Surveying
@Kushal: Could you found what is the alternative for StickyBroadcast?? Seems an interesting feature.Krantz
@Surveying what do you mean by null Broadcast Receiver here ?Peony
@stardep: Calling registerReceiver(null, yourIntentFilter).Surveying
@Surveying It would be helpful if you elaborate that use casePeony
@stardep: That seems to to be covered in the answer.Surveying
@Surveying what do you mean by last broadcast in " you get the Intent that was last broadcast for that action."Peony
@stardep: Let's use ACTION_BATTERY_CHANGED as an example. That Intent will be broadcast as the battery gains or loses charge. If you call registerReceiver() for ACTION_BATTERY_CHANGED and supply a BroadcastReceiver, that receiver will receive those broadcasts. If, instead, you call registerReceiver() with null for the receiver, the Intent that registerReceiver() returns will be the most recent ACTION_BATTERY_CHANGED broadcast.Surveying
D
57

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

Darcydarda answered 17/2, 2014 at 12:36 Comment(0)
P
11

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.

Peeress answered 21/12, 2015 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.