Understanding Broadcast Receivers in Android

Mouad OumousMouad Oumous
4 min read

Android provides a powerful component called Broadcast Receiver that allows apps to listen and respond to system-wide or app-specific events. In this article, we will explore what Broadcast Receivers are, how they work, and how to implement them effectively in an Android app.


What is a Broadcast Receiver?

A Broadcast Receiver is an Android component that listens for broadcasted messages (Intents) from the system or other apps. It enables apps to respond to different events happening on the device, such as battery status changes, network connectivity changes, or even custom app events.

How Does It Work?

  1. A broadcast is sent – This could be from the Android system (e.g., "Battery Low") or from an app.

  2. A Broadcast Receiver listens for it – If the event matches the intent filter, the receiver gets triggered.

  3. The receiver performs an action – It can show a notification, update the UI, start a service, or log data.

Common Use Cases for Broadcast Receivers

System Broadcasts (Sent by Android OS)

  • BOOT_COMPLETED → Detect when the device has restarted.

  • BATTERY_LOW → Detect low battery levels.

  • AIRPLANE_MODE_CHANGED → Detect when airplane mode is enabled/disabled.

  • CONNECTIVITY_CHANGE → Detect network connection changes.

Custom Broadcasts (Sent by apps)

  • "User Logged In" event.

  • "File Download Completed" event.

  • "New Message Received" notification in a chat app.

How to Create a Broadcast Receiver

Create a Broadcast Receiver Class

The first step is to create a class that extends BroadcastReceiver and override the onReceive() method.

class MyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        Toast.makeText(context, "Broadcast Received!", Toast.LENGTH_SHORT).show()
    }
}

Register the Receiver in AndroidManifest.xml (Static Registration)

If you want the Broadcast Receiver to always listen for a system event, register it in AndroidManifest.xml.

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

This ensures that MyReceiver is triggered when the device completes booting.

Register the Receiver in Code (Dynamic Registration)

If you want to register a receiver only when the app is running, use dynamic registration.

val receiver = MyReceiver()
val filter = IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)
registerReceiver(receiver, filter)

This is useful for handling temporary events like network changes while the app is open.

Sending Custom Broadcasts

Apps can also send custom broadcasts using sendBroadcast().

val intent = Intent("com.example.MY_CUSTOM_BROADCAST")
sendBroadcast(intent)

To receive this broadcast, the receiver must listen for "com.example.MY_CUSTOM_BROADCAST" in its intent filter.

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="com.example.MY_CUSTOM_BROADCAST"/>
    </intent-filter>
</receiver>

Types of Broadcast Receivers

TypeDescriptionExample
Manifest-registeredDeclared in AndroidManifest.xml, always available.BOOT_COMPLETED
Context-registeredRegistered dynamically in code, only active when the app is running.AIRPLANE_MODE_CHANGED

Best Practices for Using Broadcast Receivers

  • Use Context-registered receivers for short-lived events to avoid memory leaks.

  • Use Manifest-registered receivers for system events like BOOT_COMPLETED.

  • For app-specific broadcasts, use LocalBroadcastManager to ensure security.

  • Avoid long-running tasks inside onReceive()—instead, start a foreground service or work manager.

  • Be mindful of battery optimizations on Android 8+ (Oreo and above).

Real-World Example: Detecting Airplane Mode Change

Here’s a full example of how to detect when airplane mode is turned on or off.

Create a Broadcast Receiver Class

class AirplaneModeReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        val isAirplaneModeOn = intent?.getBooleanExtra("state", false) ?: return

        if (isAirplaneModeOn) {
            Toast.makeText(context, "Airplane Mode Enabled", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(context, "Airplane Mode Disabled", Toast.LENGTH_SHORT).show()
        }
    }
}

Register the Receiver Dynamically in an Activity

val receiver = AirplaneModeReceiver()
val filter = IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)
registerReceiver(receiver, filter)

This allows the app to detect changes in airplane mode while running.

Summary & Conclusion

  • A Broadcast Receiver listens for system or app-specific broadcasts.

  • It can be manifest-registered (always available) or dynamically registered (only when app is running).

  • Android provides many built-in broadcasts like BOOT_COMPLETED, BATTERY_LOW, etc.

  • Custom broadcasts allow apps to communicate with each other.

  • Always consider battery optimizations and security best practices when using broadcast receivers.

0
Subscribe to my newsletter

Read articles from Mouad Oumous directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mouad Oumous
Mouad Oumous