BroadcastReceiver for BOOT_COMPLETED is too slow
Asked Answered
G

3

13

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?

Garbo answered 15/5, 2017 at 20:48 Comment(0)
Z
13

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.

Zacharyzacherie answered 15/5, 2017 at 21:1 Comment(7)
Many Thanks. I have tried the home screen mode and it worked. But is there any way to navigate between the phone's default home screen and my customized home screen?Garbo
@mCheekati: If you mean "can I start the phone's default home screen from my app?", the answer is yes. You would need to figure out what that activity is, by using PackageManager, queryIntentActivities(), and an Intent for the home screen (ACTION_MAIN, CATEGORY_HOME). Your activity will be in the resulting list, along with 1+ others. Once you find the right activity, update your ACTION_MAIN/CATEGORY_HOME Intent with the ComponentName of the specific activity to start.Zacharyzacherie
I have fetched the list of launchers available on my phone. But unable to set the component to become a home screen. Others launchers are atleast opening(No, not as home screen), but on click of the system's default launchers shuts down the app unfortunately.Garbo
Any help with this please?Garbo
@mCheekati: Ask a separate Stack Overflow question, where you provide a minimal reproducible example showing what you are trying, and there explain, in detail, what "shuts down the app" means. For example, if you mean that an app is crashing, explain in that question which app (yours? another home screen? something else?) and, if possible, show the Java stack trace associated with the crash.Zacharyzacherie
please take a look at this #44031073Garbo
Can you elaborate "Also, starting an activity from a BroadcastReceiver at boot time is fairly evil."?Elvera
D
6

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.

Dreamadreamer answered 15/5, 2017 at 21:2 Comment(3)
I tried changing updating the priority but no change in the outcome. The home screen implementation worked. Thanks for the information though because i never knew about the priorities yet as I am a newbie to android.Garbo
Your welcome, glad the info helped a little bit and happy you got the problem resolved using the home screen implementation.Dreamadreamer
I found out that android:priority="999" needs to be put inside the <intent-filter> tag instead of the <action> tag. This did the trick for me.Armbrecht
M
1

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.

Mcclees answered 23/2, 2023 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.