Android App Auto Start on Boot: Step-by-Step Guiden in Java Code

In this tutorial, you will learn how to create an Android app in Java that automatically starts when the device boots up. This involves setting up a BroadcastReceiver to listen for the BOOT_COMPLETED action, which is broadcasted by the system after the device finishes booting. You'll also learn how to handle permissions required for this functionality, ensuring that your app can receive the BOOT_COMPLETED broadcast and launch accordingly upon device startup. This step-by-step guide will walk you through the necessary Java code and configuration to implement auto-start functionality for your Android application.
Submitted on July 07, 2024

To create an Android app that starts automatically when the device boots up, you'll need to use a combination of a broadcast receiver and handling permissions. Here's a step-by-step guide on how to achieve this:

Step-by-Step Implementation:

  1. Create a New Android Project:

    • Open Android Studio and create a new project with an Empty Activity template.
  2. Modify AndroidManifest.xml:

    • Add the necessary permissions and declare a receiver to listen for the ‘BOOT_COMPLETED’ broadcast.
        
            <?xml version="1.0" encoding="utf-8"?>
            <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.example.autostartapp">

                <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">
                    <activity android:name=".MainActivity">
                        <intent-filter>
                            <action android:name="android.intent.action.MAIN" />

                            <category android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
                    </activity>

                    <!-- Receiver to start app on boot -->
                    <receiver android:name=".BootReceiver"
                        android:enabled="true"
                        android:exported="true"
                        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                        <intent-filter>
                            <action android:name="android.intent.action.BOOT_COMPLETED" />
                            <category android:name="android.intent.category.DEFAULT" />
                        </intent-filter>
                    </receiver>
                </application>

            </manifest>
        
    

3. Create BootReceiver Class

  • Create a Java class ‘BootReceiver.java’ that extends BroadcastReceiver. This class will handle the ‘BOOT_COMPLETED’ broadcast and start your app when the device boots up.
        
            package com.example.autostartapp;

            import android.content.BroadcastReceiver;
            import android.content.Context;
            import android.content.Intent;

            public class BootReceiver extends BroadcastReceiver {

                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                        // Start your main activity or any other activity/service here
                        Intent startActivityIntent = new Intent(context, MainActivity.class);
                        startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(startActivityIntent);
                    }
                }
            }
        
    

4. Modify MainActivity

  • In your ‘MainActivity.java’, you can implement the normal functionality of your app.
        
            package com.example.autostartapp;

            import androidx.appcompat.app.AppCompatActivity;
            import android.os.Bundle;

            public class MainActivity extends AppCompatActivity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);

                    // Your app's main activity code here
                }
            }
        
    

5. Testing

    • Run the app on an emulator or a physical device.
    • After installation, restart the device to see if your app automatically starts after the boot process completes.

Notes:

  • Permissions: Ensure you have declared ‘RECEIVE_BOOT_COMPLETED’ permission in your manifest and that the user grants this permission when installing the app.
  • Broadcast Receiver: BootReceiver listens for ‘BOOT_COMPLETED’ broadcasts and starts your app. This approach is necessary due to Android's security restrictions on apps starting automatically without user interaction.
  • Testing: Test on a real device as some emulators may not handle ‘BOOT_COMPLETED’ broadcasts consistently.

Android App Auto Start on Boot: Step-by-Step Guiden in Kotlin Version

Kotlin version of an Android app that starts automatically when the device boots up. This involves creating a broadcast receiver to listen for the BOOT_COMPLETED broadcast and launching the main activity of your app.

Step-by-Step Implementation:

  1. Create a New Android Project:

    • Open Android Studio and create a new project with an Empty Activity template.
  2. Modify AndroidManifest.xml:

    • Add the necessary permissions and declare a receiver to listen for the BOOT_COMPLETED broadcast.
        
        <?xml version="1.0" encoding="utf-8"?>
            <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.example.autostartapp">

                <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">
                    <activity android:name=".MainActivity">
                        <intent-filter>
                            <action android:name="android.intent.action.MAIN" />

                            <category android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
                    </activity>

                    <!-- Receiver to start app on boot -->
                    <receiver
                        android:name=".BootReceiver"
                        android:enabled="true"
                        android:exported="true"
                        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                        <intent-filter>
                            <action android:name="android.intent.action.BOOT_COMPLETED" />
                            <category android:name="android.intent.category.DEFAULT" />
                        </intent-filter>
                    </receiver>
                </application>

            </manifest>
        
    

3. Create BootReceiver Class:

  • Create a Kotlin class ‘BootReceiver.kt’ that extends ‘BroadcastReceiver’. This class will handle the ‘BOOT_COMPLETED’ broadcast and start your app when the device boots up.
        
            package com.example.autostartapp;

            import android.content.BroadcastReceiver;
            import android.content.Context;
            import android.content.Intent;

            class BootReceiver : BroadcastReceiver() {

                @Override
                fun onReceive(context: Context, intent: Intent) {
                    if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
                        // Start your main activity or any other activity/service here
                        val startActivityIntent = Intent(context, MainActivity::class.java)
                        startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        context.startActivity(startActivityIntent)
                    }
                }
            }
        
    

4.Modify MainActivity:

  • In your MainActivity.kt, you can implement the normal functionality of your app.