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:
Create a New Android Project:
Modify AndroidManifest.xml:
<?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>
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);
}
}
}
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
}
}
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.
Create a New Android Project:
Modify AndroidManifest.xml:
<?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:
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: