I am a little confused with the documentation regarding the different types of sending broadcasts in Android. For example, there is sendStickyBroadcast()
, sendBroadcast()
, sendOrderedBroadcast()
and sendStickyOrderedBroadcast()
.
What is the difference between a sticky, normal and ordered broadcast?
There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.
:- normal broadcast intent is not available any more after this was send and processed by the system. :- the corresponding intent is sticky, meaning the intent you are sending stays around after the broadcast is complete.
A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.
When you call registerReceiver specifying an Intent Filter that matches a sticky broadcast, the return value will be the sticky broadcast Intent. To broadcast a sticky Intent your application must have the BROADCAST_STICKY uses-permission. sendStickyBroadcast(intent);
You can compare a sticky broadcast with a sticky note. Someone posts it and you can read when you pass by/your application starts - regardless of when it was posted.
An ordered broadcast is like passing a note - it passes from person/application to person/application. Anywhere in the chain the recipient can elect to cancel the broadcast preventing the rest of the chain from seeing it.
A normal broadcast.. well, just sends to everyone that's allowed & registered to listen to it.
There's a variation of broadcasts that only allow receivers registered in a running application to listen to them - i.e. a receiver in your AndroidManifest.xml will not trigger for these Intents.
An update regarding sendStickyBroadcast:
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.
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With