Broadcasting Sticky and Ordered Intents

When broadcasting an Intent using sendBroadcast, your Intent will be received by all registered Broadcast Receivers, but you cannot control the order and they cannot propagate results.

In circumstances where the order in which the Broadcast Receivers receive the Intent is important, or where you require the Receivers to be able to affect the Intent being broadcast, you can use the sendOrderedBroadcast method.

sendOrderedBroadcast(intent, null);

Using this method, your Intent will be delivered to all registered Receivers in order of priority. You can optionally assign your own Broadcast Receiver, which will then receive the Intent after it has been handled (and potentially modified) by all the other registered Broadcast Receivers.

sendOrderedBroadcast(intent, null, myBroadcastReceiver, null, Activity.RESULT_OK, null, null);

For efficiency reasons, some broadcasts are sticky. 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);

To remove a sticky intent call removeStickyBroadcast, passing in the sticky Intent to remove.

removeStickyBroadcast(intent);

Was this article helpful?

+1 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Responses

  • aloisia
    What is a sticky broadcast?
    8 years ago
  • Benjamin
    Can we have order in sticky broadcast receiver?
    8 years ago
  • aleksander
    What is broadcast sticky intents?
    9 months ago

Post a comment