4

When I restart my device, I am receiving BOOT_COMPLETED broadcast much slower (around 10-15 seconds later) than other apps.

I have seen this. But I do not think this is completely true. How (I think) I proved it wrong:

(Let's define X as an application that receives BOOT_COMPLETED broadcast faster than mine).

  1. Installed X
  2. Installed my application.
  3. Restarted device.

RESULT: X received BOOT_COMPLETED broadcast very fast. My applicated received the broadcast slow.

  1. Uninstalled both applications.
  2. Installed my application once again.
  3. Restarted device.

RESULT: My app received BOOT_COMPLETED broadcast slow again.

  1. Uninstalled my application.
  2. Installed X.
  3. Restarted device.

RESULT: X received BOOT_COMPLETED broadcast very fast once again.

CONCLUSION: My app receives BOOT_COMPLETED broadcast slow whether X is installed or not. X always receives BOOT_COMPLETED broadcast very fast. How X is being able to receive the boot completed broadcast much faster than my application even though X is installed after my application?

That is why I do not think this is completely true answer. There is something X is doing that causing it to receive BOOT_COMPLETED at a higher priority than my app.

Maybe there are some other broadcasts other than BOOT_COMPLETED which are much faster?

Any suggestions are appreciated.

Manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver 
    android:name=".receivers.BootCompletedIntentReceived">
    <intent-filter android:priority="2147483647">
        <category android:name="android.intent.category.DEFAULT"/>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        <!--For HTC devices-->
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
    </intent-filter>
</receiver>

BootCompletedIntentReceived.kt

class BootCompletedIntentReceived: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        //DO SOMETHING HERE
    }
}
Anatolii
  • 14,139
  • 4
  • 35
  • 65
Azizjon Kholmatov
  • 1,136
  • 1
  • 13
  • 26

3 Answers3

0

The value of priority of intent-filter must be between -1000 and 1000.
Try this:

<intent-filter android:priority="999">
sweid337
  • 26
  • 1
  • 4
0

I suspect that the app X is a priveledged application. From the documentation for android:priority attribute:

In certain circumstances the requested priority is ignored and the value is capped to 0. This occurs when: A non-privileged application requests any priority >0

So if X is a priveledged app and it requests a high priority, it will be granted, and your app, being non-priveledged, will always get the broadcast later.

Rediska
  • 1,392
  • 10
  • 14
0

Experiment

I created 2 sample apps - app1 and app2, each of which registered its own BroadcastReceiver for receiving an android.intent.action.BOOT_COMPLETED. So, BroadcastReceiver for app1 had a priority of 10 and the other - 20.

app1

...
    <receiver
        android:name="com.example.app1.BootCompletedIntentReceiver">
        <intent-filter android:priority="10">
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
...

app2

...
    <receiver
        android:name="com.example.app2.BootCompletedIntentReceiver">
        <intent-filter android:priority="20">
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
...

Result

2021-03-30 18:14:07.337 3453-3453/com.example.app1 E/Anatolii: BootCompletedIntentReceiver called

2021-03-30 18:14:07.509 3498-3498/com.example.app2 E/Anatolii: BootCompletedIntentReceiver called

As it can be seen from the output, app1 received the intent earlier, event though it had a higher priority.

Answer

Your android:priority doesn't matter because android.intent.action.BOOT_COMPLETED is probably not sent in the synchronous way, and so priority is ignored as stated in the documentation:

android:priority ... 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.)...

Anatolii
  • 14,139
  • 4
  • 35
  • 65