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?

Marco Gallella
  • 793
  • 1
  • 11
  • 18
  • Im looking for similar functionality, if you succeed please share the code with me. – Anirudh Jan 24 '13 at 07:25
  • Does this answer your question? [How do I start my app when the phone starts on Android?](https://stackoverflow.com/questions/6391902/how-do-i-start-my-app-when-the-phone-starts-on-android) – coding Bott Feb 13 '23 at 08:33

5 Answers5

87

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);
   }

}
Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
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>
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71
  • 1
    Tried, doesn't work, after showing the normal startup screen my intent is being called after 2 seconds approx. – Anirudh Jan 24 '13 at 07:30
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>
Hadži Lazar Pešić
  • 1,031
  • 1
  • 13
  • 20
2

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.

alperk01
  • 183
  • 1
  • 7
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

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141