user8000126
user8000126

Reputation:

BroadcastReceiver for BOOT_COMPLETED is too slow

The below is my manifest file.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mccheekati.test_trail">
    <uses-permission 
    android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

   <receiver 
   android:name="com.example.mccheekati.test_trail.yourActivityRunOnStartup"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" 
           />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The Broadcast receiver is as follows:

    public class yourActivityRunOnStartup extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

}

No errors. The application is opening on rebooting the phone. But it takes a minute's time to launch the application after reboot. Is there any what to start the application immediately after reboot?

Upvotes: 13

Views: 7920

Answers (3)

Yeung
Yeung

Reputation: 2231

In my case, on an Android 11 device

<intent-filter>
       <category android:name="android.intent.category.DEFAULT" />
       <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>

Without <category> tag , after boot, it take about 1 minute for the receiver to receive android.intent.action.BOOT_COMPLETED , while with <category> tag, it only takes 4 seconds

I know this question 's example already has this line, just to share my case.

Upvotes: 1

Mode-X
Mode-X

Reputation: 81

There will be some system resources that need to boot first and will have a higher priority over your receiver. However you could try setting a priority to your intent in the manifest. Like so:

<intent-filter android:priority="999">
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />

Please have a look at the details from the developer docs regarding this: Docs

Extract regarding 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.)

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.

Upvotes: 6

CommonsWare
CommonsWare

Reputation: 1007296

Is there any what to start the application immediately after reboot?

No.

There are many, many apps that want to get control at boot time. How quickly yours will get its turn will depend on many variables, such as the number of installed apps, the CPU speed of the device, the amount of system RAM on the device, etc.

Also, starting an activity from a BroadcastReceiver at boot time is fairly evil. If you want to be the first thing the user sees after a reboot, write a home screen implementation.

Upvotes: 13

Related Questions