Manifest-registered Broadcast Receivers
Manifest-registered broadcast receivers are broadcast receivers that are registered in your AndroidManifest.xml
.
Characteristics​
Compared to context-registered one, manifest-registered broadcast receiver has these characteristics:
- They are constrained by implicit broadcast exceptions introduced in Android 8.0 (API level 26). Only several broadcasts are exempted from these limitation.
Basic Usage​
Let's say we have a simple BroadcastReceiver
subclass that listen to airplane mode changes:
AirplaneModeBroadcastReceiver.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_AIRPLANE_MODE_CHANGED] intent action.
*/
class AirplaneModeBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val state = intent.getBooleanExtra("state", false)
Log.i("TAG", "Airplane mode is: $state")
}
}
To register this broadcast receiver to your application, simply add this to your 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">
<application>
<receiver
android:name=".AirplaneModeBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>
</application>
</manifest>
The system package manager will register the broadcast receiver when the application is installed.