Ordered Broadcast

Android Ordered Broadcast sendOrderedBroadcast

Ordered broadcast is compared with normal unordered broadcast (sendBroadcast). It is a broadcast delivered serially one by one and a frequently asked interview question.

1. Features of Ordered Broadcast

  1. Serial execution (sequential delivery): Receivers receive broadcasts one after another in order. The next receiver will only get the broadcast after the previous one finishes processing; they do not receive it simultaneously.
  2. Broadcast can be aborted: Call abortBroadcast() inside receiver onReceive() to terminate the broadcast. All subsequent receivers will no longer receive this broadcast.
  3. Priority determines receiving order: android:priority. Higher numeric value means higher priority and earlier broadcast reception.
    • Value range: -1000 ~ 1000
    • Example: demo1 has priority=100, demo2 uses a smaller value. Then demo1 receives first, demo2 receives later
  4. Receivers can carry and modify broadcast data and pass it onward.

Note: Unordered broadcast sendBroadcast() cannot be aborted and has no priority. Multiple receivers receive the broadcast almost at the same time

2. Code to Send Ordered Broadcast (Sender Demo1)

Intent intent = new Intent("com.foxdevelop.www.senddemo.MY_BROADCAST");
// Send ordered broadcast. The second parameter is permission (null means no permission restriction)
sendOrderedBroadcast(intent, null);
Code language: PHP (php)

3. Statically Registered Receiver with Priority, AndroidManifest.xml

<receiver android:name=".MyReceiver">
    <!-- priority: higher number gets broadcast earlier -->
    <intent-filter android:priority="100">
        <action android:name="com.foxdevelop.www.senddemo.MY_BROADCAST"/>
    </intent-filter>
</receiver>
Code language: HTML, XML (xml)

Set a priority smaller than 100 for the receiver in Demo2, for example android:priority="50". Execution order: Demo1 Receiver → Demo2 Receiver

4. Abort Broadcast abortBroadcast()

public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Received our custom broadcast", Toast.LENGTH_LONG).show();
    // Abort ordered broadcast! Receivers with lower priority afterwards will never receive this broadcast
    abortBroadcast();
}
Code language: JavaScript (javascript)

Key Points:

  • abortBroadcast() only works for ordered broadcasts; calling this method on regular unordered broadcasts has no effect.
  • Once aborted, the broadcast delivery chain terminates.

5. Full Execution Flow of Ordered Broadcast

  1. Demo1 calls sendOrderedBroadcast to send broadcast
  2. System finds all broadcast receivers matching the Action, sorted by priority descending
  3. Receiver with priority 100 (Demo1 Receiver) receives first → execute onReceive
    • If abortBroadcast() is called here, broadcast terminates directly, Demo2 (priority50) cannot receive broadcast
    • If abortBroadcast is not called, broadcast continues to Demo2 Receiver
  4. Demo2 Receiver executes onReceive

Comparison Table

FeatureUnordered Broadcast sendBroadcastOrdered Broadcast sendOrderedBroadcast
Receiving OrderParallel, received almost at the same time, no sequenceSerial, sorted by priority, higher priority receives first
Broadcast AbortCannot abort, abortBroadcast has no effectCan call abortBroadcast to abort
Data DeliveryCannot pass modified data to next receiverCan modify Intent data and pass onward

Ordered Broadcast

Leave a Reply

Your email address will not be published. Required fields are marked *