Jiakan Wang
Jiakan Wang

Reputation: 262

Does Android system send BOOT_COMPLETED intent multiple times to multiple receiver in one process or just once?

So basically I have two major functionality, A and B in my app, and they both do something on boot. So I made two BroadcastReceiver R1 and R2, both of which receives BOOT_COMPLETED intent. R1 has fairly complicated logic and handles functionality A. R2's logic is really simple and it handles B.

My problem is to decide if I should keep the code cleaner by keeping both Broadcast Receivers and make each of them listen to BOOT_COMPLETED intent, or if I should combine them to increase performance?

How big is the performance hit to receive two of the same intent instead of one? Will the send-receive-intent process happens once or twice in my app?

Also, is BOOT_COMPLETED sent only after the boot is completely finished, when user can launch apps?

Edit: After testing, the difference between receiving the same intents in the same app by two components vs. by one is roughly only a couple milliseconds.

Upvotes: 2

Views: 3955

Answers (3)

dleal
dleal

Reputation: 660

You could use the android:priority property in order to control these receiver, and make them execute in a specific order. The activity with the highest priority number will execute first. Something like this:

<activity
     android:name="First activity">
     <intent-filter android:priority="10" >
          <action android:name="android.intent.action.DEVICE_INITIALIZATION_WIZARD" />
     </intent-filter>
</activity>

<activity
     android:name="Second activity">
     <intent-filter android:priority="1" >
          <action android:name="android.intent.action.DEVICE_INITIALIZATION_WIZARD" />
     </intent-filter>
</activity>

You have information about this in this link

android:priority The priority that should be given to the parent component with regard to handling intents of the type described by the filter. This attribute has meaning for both activities and broadcast receivers: It provides information about how able an activity is to respond to an intent that matches the filter, relative to other activities that could also respond to the intent. When an intent could be handled by multiple activities with different priorities, Android will consider only those with higher priority values as potential targets for the intent. It controls the order in which broadcast receivers are executed to receive broadcast messages. Those with higher priority values are called before those with lower values. (The order applies only to synchronous messages; it's ignored for asynchronous messages.) Use this attribute only if you really need to impose a specific order in which the broadcasts are received, or want to force Android to prefer one activity over others.

The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.

Also see setPriority().

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1007554

In addition to Sam's answer...

Also, is BOOT_COMPLETED sent only after the boot is completely finished, when user can launch apps?

It is sent around the time of when the keyguard appears. The key is "around the time". There are many boot-time receivers, and they will all receive the broadcast in an indeterminate order. Please make no assumptions about the precise time that you will get the broadcast or the precise state of the system at that point (e.g., may not have an Internet connection).

Upvotes: 3

Sam
Sam

Reputation: 86958

BroadcastReceivers are inherently independent of any Activities.

  • If Activity A and Activity B are part of the same Application, I suggest combining them and using only one BroadcastReceiver.
  • If they are for different apps and you plan on releasing the apps separately then they should remain two different entities.

Upvotes: 1

Related Questions