On Android, how does a manifest-declared broadcast receiver differ from a context-registered one, and what do RECEIVER_EXPORTED and RECEIVER_NOT_EXPORTED decide?
answer
- install time vs a Context's lifetime
- can it start a stopped app
- implicit broadcasts since API 26
- who may send to it
- onReceive is brief
basics
~20 sA manifest-declared receiver is registered at install and can start your app, but from API 26 most implicit broadcasts skip it; a context-registered receiver lives as long as its Context. RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED says whether other apps may reach it.
solid answer
~50 sA receiver declared with `<receiver>` in the manifest is registered when the app is installed and is a separate entry point: the system starts the app to deliver a matching broadcast. Apps targeting Android 8.0 (API 26) or higher cannot use it for most implicit broadcasts, apart from an exemption list such as `ACTION_BOOT_COMPLETED` and `ACTION_LOCALE_CHANGED`. A context-registered receiver, added with `registerReceiver()`, hears broadcasts only while its `Context` is valid and must be unregistered to avoid a leak. Its export flag decides who can send to it: `RECEIVER_EXPORTED` for broadcasts from other apps or privileged system apps, `RECEIVER_NOT_EXPORTED` for your own app's broadcasts. Apps targeting Android 14 (API 34) must pass one of them for receivers that hear anything beyond system broadcasts, and `ContextCompat.registerReceiver()` accepts the flags on older releases. Either way `onReceive()` runs on the main thread by default and must return quickly.
code
kotlin · 34 linesclass TripActivity : AppCompatActivity() {
private val tripUpdated = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val tripId = intent.getStringExtra("tripId") ?: return
refreshTrip(tripId)
}
}
override fun onStart() {
super.onStart()
ContextCompat.registerReceiver(
this,
tripUpdated,
IntentFilter("com.example.ride.TRIP_UPDATED"),
ContextCompat.RECEIVER_NOT_EXPORTED,
)
}
override fun onStop() {
unregisterReceiver(tripUpdated)
super.onStop()
}
private fun refreshTrip(tripId: String) { /* reload the trip from the repository */ }
}
// Sender, elsewhere in the same app: only this package can match
fun notifyTripUpdated(context: Context, tripId: String) {
context.sendBroadcast(
Intent("com.example.ride.TRIP_UPDATED")
.setPackage(context.packageName)
.putExtra("tripId", tripId),
)
}go deeper
Recall the two registration styles, that a manifest receiver can start the app, and that a runtime receiver must be unregistered when its Context goes away.
Explain the API 26 implicit-broadcast rule and its exemptions, the two export flags and the API 34 requirement, and why onReceive() must stay short.
Show the production judgement: setPackage() and permissions on app-internal broadcasts, exported receivers validating input, and broadcast timing changes on Android 14 and 16.
Decide which events the app should learn through broadcasts at all, and make not-exported, package-scoped broadcasts the default the team reviews against.
## What a broadcast receiver is A **broadcast** is an intent sent to every interested receiver rather than to one component: the system announces events such as a locale change or boot completion, and apps can send their own. A **`BroadcastReceiver`** subscribes with an intent filter and handles each delivery in `onReceive(Context, Intent)`. There are two ways to register one, and they behave differently. ## Manifest-declared receivers - Declared with a `<receiver>` element and an `<intent-filter>` in `AndroidManifest.xml`; the package manager registers it at **install time**. - It is a **separate entry point**: if the app is not running, the system starts its process to deliver the broadcast. - Since **Android 8.0 (API level 26)**, apps that target 26 or higher **cannot** use the manifest for most **implicit broadcasts** (broadcasts not sent to the app specifically). A short exemption list remains, including `ACTION_BOOT_COMPLETED`, `ACTION_LOCKED_BOOT_COMPLETED`, `ACTION_LOCALE_CHANGED`, `ACTION_TIMEZONE_CHANGED` and some USB, Bluetooth and telephony events. - Its `android:exported` attribute decides whether other apps, and broadcasts sent by the system, can reach it; set it explicitly every time. ## Context-registered receivers - Registered in code with `registerReceiver()` (or `ContextCompat.registerReceiver()`) and an `IntentFilter`. - It receives broadcasts only while the registering **`Context` is valid**: registered from an `Activity`, it lives while that activity does; registered from the `Application`, while the process runs. - It holds a reference to that `Context`, so pair every registration with `unregisterReceiver()`, for example `onStart()` with `onStop()`, or the receiver leaks. - It can hear implicit broadcasts the manifest can no longer receive, because it only works while the app is running. ## What the export flags decide | Flag | Who can reach the receiver | |---|---| | `RECEIVER_EXPORTED` | other apps, including your own other apps, and broadcasts from privileged system apps such as Bluetooth and telephony | | `RECEIVER_NOT_EXPORTED` | your own app, plus some system broadcasts, but not those sent by privileged apps outside the system UID | Apps that **target Android 14 (API level 34)** or higher must pass one of the two flags when they register a receiver at runtime for broadcasts other than system ones; registering without a flag fails with a `SecurityException`. `ContextCompat.registerReceiver()` takes the flags on every API level the app supports. If one receiver would need both, split it into two receivers. A manifest declaration for a broadcast the system still delivers looks like this; it is exported because the sender is the system, so `onReceive()` should check the action before acting: ```xml <receiver android:name=".LocaleChangedReceiver" android:exported="true"> <intent-filter> <action android:name="android.intent.action.LOCALE_CHANGED" /> </intent-filter> </receiver> ``` ## Choosing between the two - Use a **manifest receiver** for an exempt system broadcast, or for a broadcast sent **specifically to your app**: an explicit intent, such as the one inside a notification action's or an alarm's `PendingIntent.getBroadcast()` token, still reaches it, because the API 26 rule only covers implicit broadcasts. - Use a **context-registered receiver** for anything that matters only while the user is in the app, and for implicit broadcasts the manifest can no longer receive. ## onReceive() is brief For a manifest receiver the system creates a new receiver object for each delivery, valid only until `onReceive()` returns; for either kind, the callback runs on the **main thread** by default. Once it returns, a process that hosts nothing but that receiver may be killed to reclaim memory, taking any thread it started with it. So: 1. Read the intent, validate its extras, and do only quick work in `onReceive()`. 2. Hand longer work to scheduled background work (the WorkManager lesson covers it) instead of starting a thread. ## Sending safely, and what recent releases changed - `sendBroadcast()` delivers to all matching receivers in no defined order; `sendOrderedBroadcast()` delivers one at a time, and a receiver may abort it. - Call `setPackage()` on an app-internal broadcast so no other app's receiver can match it, and pass a permission to `sendBroadcast()` when only holders of that permission should receive it. - **Android 14:** while an app is in the cached state, the system defers less important broadcasts and delivers them when the app becomes active again. - **Android 16:** `android:priority` and `IntentFilter.setPriority()` are respected only within one process, no longer across processes. ## In a ride-hailing app The trip screen registers a `RECEIVER_NOT_EXPORTED` receiver for the app's own "trip updated" action between `onStart()` and `onStop()`, and the sender calls `setPackage()`. A manifest receiver for `ACTION_LOCALE_CHANGED`, which stays exempt, refreshes cached place names.
- Your app needs to react to connectivity changes. Why does a manifest receiver for CONNECTIVITY_ACTION not work?Apps targeting Android 7.0 (API 24) or higher must register `CONNECTIVITY_ACTION` with `registerReceiver()`; the manifest declaration is ignored. It is also an implicit broadcast, which the API 26 rule keeps out of manifest receivers. Listen at runtime while the app is in use instead.
- When does a context-registered receiver need RECEIVER_EXPORTED even though no other app of yours sends to it?When it listens for system broadcasts sent by privileged apps that are part of the framework but do not run under the system UID, such as Bluetooth or telephony. A `RECEIVER_NOT_EXPORTED` receiver gets some system broadcasts but not those, so it must be exported, and its `onReceive()` should validate what arrives.
- A manifest receiver still gets most implicit broadcasts on apps targeting API 26 or higher
- A receiver registered in an Activity keeps working after the Activity is destroyed
- onReceive() runs on a background thread, so blocking work is fine there
- RECEIVER_NOT_EXPORTED receivers also hear broadcasts from privileged apps like Bluetooth
- Starting a thread in onReceive() keeps the process alive until it finishes
Assumes Android 17 (API 37); manifest receivers lost most implicit broadcasts for apps targeting API 26, runtime receivers need an export flag for apps targeting API 34, and Android 16 limits broadcast priority to one process.