How to Launch an Application Automatically on System Boot Up in Android?
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.
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.
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.
Then restart your phone/emulator. It will automatically restart based on how much time your system takes to boot up.
Please Login to comment...