How to start/ launch application at boot time Android
Asked Answered
T

5

44

I would like to launch my app when my tablet starts, so that the main activity of my app is the first thing that the user see when they start the tablet.
I've read about LauncherActivity but I don't understand how to use it.
Can anyone help me with suggestions, links or tutorials for this?
Is LauncherActivity the best way or are there alternatives?

Testerman answered 3/5, 2012 at 9:22 Comment(2)
Im looking for similar functionality, if you succeed please share the code with me.Damicke
Does this answer your question? How do I start my app when the phone starts on Android?Mayers
G
90

These lines of code may be helpful for you...

Step 1: Set the permission in AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Step 2: Add this intent filter in receiver

<receiver android:name=".BootReceiver">
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

Step 3: Now you can start your application's first activity from onReceive method of Receiver class

public class BootReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
       Intent myIntent = new Intent(context, MainActivity.class);
       myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(myIntent);
   }

}
Godson answered 3/5, 2012 at 9:32 Comment(8)
thaks vishesh, morevore this steps my main activity maust extends LauncherActivity or simply Activity?Testerman
Instead that you can use Receiver, that will work fine... check new updated line of code..Godson
yes that will be your main activity.. from there your application will start.Godson
if my post is useful and clear please rate me!! So monday i can try this solution and after that i accept an aswer ;)Testerman
change your title like, "How to start/ launch application at boot time"...:-)Godson
this doesn't work for me. I put permission and receive on Manifest. Create BootReciever iquals you dued, but doesn't work. I made a test with <action android:name="android.intent.action.AIRPLANE_MODE"/> and its work, but when a restart my device noting hapen.Tympanitis
but for that we have to run the app once so that boot receiver broadcast will register. Otherwise app will not run when device boot.Kuomintang
I have checked in MI device as well in Samsung also but it didn't work, app not autolaunch after reboot. Can you help?Sera
S
15

If you want to start the app when the tablets starts, you need to listen to the BOOT_COMPLETED action and react to it. BOOT_COMPLETED is a Broadcast Action that is broadcast once, after the system has finished booting. You can listen to this action by creating a BroadcastReceiver that then starts your launch Activity when it receives an intent with the BOOT_COMPLETED action.

Add this permission to your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Create a Custom BroadcastReceiver in your project:

public class MyBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            Intent i = new Intent(context, MyActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
} 

Then modify your manifest file by adding the BroadCastReceiver to the Manifest:

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
Spotter answered 3/5, 2012 at 9:47 Comment(1)
Tried, doesn't work, after showing the normal startup screen my intent is being called after 2 seconds approx.Damicke
G
5

Answer by @vishesh chandra is correct. But on some device doesn't work because app was installed on external storage by default. Please ensure that you specify

android:installLocation="internalOnly"

otherwise you will not receive any Boot Complete actions if the app is installed in the SD card. Add this into application tag in manifest.xml file and it will work.

Usage:

<application
        android:name=".Data.ApplicationData"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:installLocation="internalOnly"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
        <!--activities, services...-->
</application>
Geomancy answered 20/3, 2017 at 9:49 Comment(0)
A
5

After trying BootReceiver code, you will see that your app will not start after boot. Android restricts starting activities from the background.

https://developer.android.com/guide/components/activities/background-starts

The working way to start activity after boot is to give SYSTEM_ALERT_WINDOW permission to the app. In the AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

In the MainActivity, add this request permission code. This will open a settings window. User must enable the permission for your app.

// To start app after boot
private void startSystemAlertWindowPermission(){
    try{
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(! Settings.canDrawOverlays(this)) {
                Log.i(TAG, "[startSystemAlertWindowPermission] requesting system alert window permission.");
                startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"+getPackageName())));
            }
        }
    }catch (Exception e){
        Log.e(TAG, "[startSystemAlertWindowPermission] error:", e);
    }
}

If the user gives permission, your MainActivity will start after boot.

If your app does not open an activity, but just opens a background service, this permission is not needed.

Alteration answered 19/8, 2022 at 8:40 Comment(1)
Thank you so much, I totally forget about the SYSTEM_ALER_WINDOW permissionCaulk
B
1

I would like to add one point in this question which I was facing for couple of days. I tried all the answers but those were not working for me. If you are using android version 5.1 please change these settings.

If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.

settings> app > your app > Restrict to launch (dis-select)

please see picture.

Image

Bunkum answered 30/8, 2017 at 6:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.