StackTips
 2 minutes

How to Start an Application at Device Bootup in Android

By Nilanchala @nilan, On Jan 11, 2024 Android 3.89K Views

This tutorial will explain how to start an application while the Android device bootup. For this, we need to listen to the BOOT_COMPLETED action and react to it. The 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.

Here's a step-by-step guide:

Add permission to your manifest

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

Create a Custom BroadcastReceiver

Secondly, create a custom BroadcastReceiver in your project to receive boot up event.

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) {
		Intent i = new Intent(context, MainActivity.class);  
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);  
	}
}

Adding BroadCastReceiver to the Android Manifest

<receiver android:name=".BootReceiver" android:enabled="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>

Install the application, and then restart the device. You can see the application will start after the device restarts.

nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.