If 2 broadcasts, A and B, and sent in that order, does Android guarantee that all interested receivers will receive them in the same order?
4 Answers
I believe you are asking if there is any way to guarantee that each interested receiver will receive its respective Broadcast A before getting Broadcast B. The best answer I can give is that it is highly likely, because all broadcasts are passed to the ActivityManager
which should handle them in turn. But I don't believe there is anything in the framework that "guarantees" this behavior (I can't find a queue on ActivityManager where they are all posted or anything like that). Also, BroadcastReceiver
will only handle one Intent at a time, which helps.
If instead you meant "can I control the order of receivers in which each broadcast is sent", then your answer lies with sendOrderedBroadcast()
as other have eluded to.

- 62,780
- 12
- 127
- 139
-
Just out of curiosity, you don't have any updates for this answer, do you? I'm very curious about the same thing at the moment. Thanks! – Patrick87 Nov 07 '12 at 00:25
Some more info about this question may be found here:
Broadcast receiver execution.
https://groups.google.com/forum/#!topic/android-developers/ClIGNuGJUts
In the above link Dianne Hackborn writes ...
"A particular receiver can only process one broadcast at a time. As each broadcast happens, it is processed to determine the targets it should go to, and dispatched into the message queue for each target. When a later broadcast is sent, it will generally not be moved ahead of the queue.
For the screen on/off broadcasts, you can rely on the most recent broadcast you get being the current state of the screen. once everything has settled down. You can't rely on ordering with any other broadcasts, or other broadcast receivers that may also be receiving the broadcasts."
This article suggests possibly using sendBroadcastSync() for synchronous delivery, but it is not guaranteed.
Does LocalBroadcastManager deliver events in the order in which the events wer sent?
-
This should be the upvoted and accepted answer, since it has official references. – Blaisorblade Sep 19 '16 at 11:33
You could have the behavior check for broadcast A and if isn't there, don't use B until A happens? So they might be received backwards, but still acted on properly?

- 2,779
- 5
- 23
- 34
-
How do check the behavior of the broadcast? Is this done by the receiver? Thanks. – John W Jul 19 '11 at 17:36
-
A variation on this idea could be that the broadcast sender inserts an incrementing sequence number within the `Intent` of each broadcast it sends out. This way, receivers can detect whether consecutive broadcasts were received in a different order to that in which they were transmitted, and take appropriate action. This could be a good belt-and-braces safety measure to use, until someone proves the Android system guarantees order (which would be better). – Trevor Aug 05 '12 at 20:33
You might want to use sendOrderedBroadcast (if you have control of this) to guarantee order.

- 9,639
- 36
- 41
-
2Though I can see where the confusion is, unfortunately this isn't a solution because `sendOrderedBroadcast`provides a means of ensuring a given single broadcast is consumed by receivers in a priority order. It does not unfortunately have anything to do with preserving the order in which multiple separate broadcasts are received, which is what the question relates to. – Trevor Aug 05 '12 at 20:24