Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Android broadcasts received in order?

Tags:

android

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?

like image 272
John W Avatar asked Jul 18 '11 18:07

John W


People also ask

How does broadcast receive work?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

How broadcast receiver is implemented in Android?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

What is the life cycle of broadcast receivers in Android?

When a broadcast message arrives for the receiver, Android calls its onReceive() method and passes it the Intent object containing the message. The broadcast receiver is considered to be active only while it is executing this method. When onReceive() returns, it is inactive.

How many broadcast receivers are there in Android?

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.


2 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.

like image 198
devunwired Avatar answered Oct 14 '22 05:10

devunwired


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?

like image 42
beyeriii Avatar answered Oct 14 '22 05:10

beyeriii