Open In App

How to Launch an Application Automatically on System Boot Up in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

In many devices, we need our App to launch automatically when the device is started. In this article, we see how to add the auto-launch functionality to the App on the system boot. System Boot is а  stаrt-uр sequenсe thаt stаrts the орerаting system оf our device when it is turned оn. This annoying feature for someone who uses its device for basic purposes like calling the phone, watching a movie, etc. The use case of auto-launch is that like suppose we have a robot with an android device on his body and on that android device there is one app through which the robot works so what we need is that when we start the robot the android device also starts and app through which it is controlled it automatically start or we see auto-launch so in that case, we need that this type of functionality.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Create a New Class startupOnBootUpReceiver

Create a Class startupOnBootUpReceiver which extends the BroadcastReceiver

Android Applications can send or receive broadcast messages from the Android system and other Android Applications. The system sends various broadcasts when an event of interest occurs. For example, when the system boots, when airplane mode is active, when the device is silent, or when there is an incoming call or text message. Broadcast receivers are used to respond to these system events. Broadcast receivers allow you to subscribe to any system or custom event. When this event occurs, the appropriate recipients will be notified. Refer to Broadcast Receiver in Android With Example.

There are various system intents through which the system interacts.

android.intent.action.BOOT_COMPLETED

It sends a message to the system that the boot has been completed. So using this broadcast we can call our MainActivity which means that when the system boot is completed then the app launches. Add the following code.

Java




import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
  
public class startupOnBootUpReceiver extends BroadcastReceiver {
  
    @Override
    public void onReceive(Context context, Intent intent) {
          
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
              
            Intent activityIntent = new Intent(context, MainActivity.class);
              
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              
            context.startActivity(activityIntent);
        }
    }
}


Kotlin




import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
  
class startupOnBootUpReceiver : BroadcastReceiver() {
  
    override fun onReceive(context: Context, intent: Intent) {
  
        if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
  
            val activityIntent = Intent(context, MainActivity::class.java)
  
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
              
            context.startActivity(activityIntent)
        }
    }
}


Step 3: Adding Permission to AndroidManifest.xml File

Navigate to app > manifests > AndroidManifest.xml and add the piece of code given below to the file.

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

Step 4: Working with the XML File

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksforGeeks"
        android:textStyle="bold"
        android:textSize="34sp"
        android:textColor="#0F9D58"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


Output:

Then restart your phone/emulator. It will automatically restart based on how much time your system takes to boot up.

Launch an Application Automatically on System Boot Up

 



Last Updated : 22 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads