Skip to main content

Booting Completed

Whenever user finishes booting their device, certain broadcasts are sent by the system:

  1. ACTION_LOCKED_BOOT_COMPLETED: Before user unlocks the device.
  2. ACTION_BOOT_COMPLETED: After user unlocks the device.

ACTION_LOCKED_BOOT_COMPLETED​

This broadcast is sent by the system once the user has finished booting, but while still in the "locked" state. It can be used to perform application-specific initialization, such as installing alarms. Upon receipt of this broadcast, the user is still locked and only device-protected storage can be accessed safely.

Basic Example​

The first thing we need to do is adding RECEIVE_BOOT_COMPLETED permission to our AndroidManifest.xml file:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

</manifest>

Then, let's create a simple BroadcastReceiver subclass that will log a message when it receives the broadcast. In real app, you might want to add initialization logic in this class.

LockedBootCompletedBroadcastReceiver.kt
package com.hanmajid.androidnotebook

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

/**
* Simple broadcast receiver that listens to [Intent.ACTION_LOCKED_BOOT_COMPLETED]
* intent action.
*/
class LockedBootCompletedBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_LOCKED_BOOT_COMPLETED) {
// TODO Initialize your application's alarms.
Log.i("TAG", "Hello Android Notebook!")
}
}
}

Finally, we need to register the above BroadcastReceiver inside our AndroidManifest.xml file. Make sure to add the ACTION_LOCKED_BOOT_COMPLETED intent action in the <intent-filter> tag and set android:directBootAware to true:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application>

<receiver
android:name=".LockedBootCompletedBroadcastReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

</manifest>

ACTION_BOOT_COMPLETED​

This broadcast is sent by the system once the user has finished booting and the user has unlocked the device. We can use this broadcast to perform our app's initialization tasks, such as installing alarms. Upon receipt of the broadcast, the user is unlocked and both device-protected and credential-protected storage can be accessed safely.

Basic Example​

The first thing we need to do is adding RECEIVE_BOOT_COMPLETED permission to our AndroidManifest.xml file:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

</manifest>

Then, let's create a simple BroadcastReceiver subclass that will log a message when it receives the broadcast. In real app, you might want to add initialization logic in this class.

BootCompletedBroadcastReceiver.kt
package com.hanmajid.androidnotebook

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

/**
* Simple broadcast receiver that listens to [Intent.ACTION_BOOT_COMPLETED]
* intent action.
*/
class BootCompletedBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
// TODO Initialize your application's alarms.
Log.i("TAG", "Hello Android Notebook!")
}
}
}

Finally, we need to register the above BroadcastReceiver inside our AndroidManifest.xml file. Make sure to add the ACTION_BOOT_COMPLETED intent action in the <intent-filter> tag:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application>

<receiver
android:name=".BootCompletedBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

</manifest>

References​