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
- 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.
- Broadcast can be aborted: Call
abortBroadcast()inside receiveronReceive()to terminate the broadcast. All subsequent receivers will no longer receive this broadcast. - 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
- Value range:
- 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
- Demo1 calls
sendOrderedBroadcastto send broadcast - System finds all broadcast receivers matching the Action, sorted by priority descending
- 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
- If
- Demo2 Receiver executes onReceive
Comparison Table
| Feature | Unordered Broadcast sendBroadcast | Ordered Broadcast sendOrderedBroadcast |
|---|---|---|
| Receiving Order | Parallel, received almost at the same time, no sequence | Serial, sorted by priority, higher priority receives first |
| Broadcast Abort | Cannot abort, abortBroadcast has no effect | Can call abortBroadcast to abort |
| Data Delivery | Cannot pass modified data to next receiver | Can modify Intent data and pass onward |
Ordered Broadcast
Next: Android Local Broadcast