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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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? –  May 15 '17 at 23:23
  • @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. – CommonsWare May 15 '17 at 23:33
  • 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. –  May 16 '17 at 21:43
  • Any help with this please? –  May 17 '17 at 16:55
  • @mCheekati: Ask a separate Stack Overflow question, where you provide a [mcve] 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. – CommonsWare May 17 '17 at 17:01
  • please take a look at this http://stackoverflow.com/questions/44030573/unable-to-launch-default-home-screen-from-a-list-of-all-launchers –  May 17 '17 at 17:08
  • Can you elaborate "Also, starting an activity from a BroadcastReceiver at boot time is fairly evil."? – Cengiz Kandemir Jan 10 '18 at 12:56
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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mode-X
  • 81
  • 1
  • 6
  • 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. –  May 15 '17 at 23:26
  • Your welcome, glad the info helped a little bit and happy you got the problem resolved using the home screen implementation. – Mode-X May 16 '17 at 04:36
  • 3
    I found out that android:priority="999" needs to be put inside the tag instead of the tag. This did the trick for me. – Hristo Stoyanov Mar 05 '19 at 16:24
0

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.

Yeung
  • 2,202
  • 2
  • 27
  • 50