Go back

Broadcast Receivers in Android

Published on 10 May, 2025

Suppose, you were using your favourite app. There was a notification and you bring the notification bar down and accidently switched on the Airplane Mode, you go back to app and you see a message like Airplane Mode Enabled. Turn it off. OR Connection not available. Turn off airplane mode.

Ever wondered how your favorite app instantly knows the entire device went offline when you switch on Airplane Mode?

Well that’s Broadcast Receivers in action.

What is a Broadcast

Before we learn about what Broadcast Receivers are first let’s learn about what is a Broadcast.

A Broadcast is a message or an information about a specific event that the Android OS, or sometimes apps send to all the other apps installed on your system.

These messages includes important things like if the airplane mode is toggled or maybe Device has been restarted. Now You might think that if OS sends these messages to all the apps, then why not all apps show like Airplane Mode Enabled message!

Good Question! While this is true that every app receives these broadcast messages, however apps have to volunteer themselves to respond to these messages.

So an app literally have to register itself and volunteer to listen to specific events or messages sent by the system and then respond accordingly.

And now you might be asking ; How exactly does these apps volunteer for listening and responding to these specific events?

Well answer is Broadcast Receivers.

Broadcast Receivers

A Broadcast Receiver is an android component that enables your app to listen and respond to system or app generated broadcasts.

Think of broadcast receivers like a radio antenna which is tuned to a specific frequency. When a broadcasts on the same matching frequency, the receivers picks it and then react accordingly as specified in the code.

Why Use a Broadcast Receiver?

  1. React to system events: Respond when the device boots, network connectivity changes, battery gets low, and more.

  2. Communicate between apps: One app can send a broadcast that others pick up—though this is less common now due to security.

  3. Keep your app lightweight: Many tasks only need to run when a certain event happens, so you don’t need a constantly running service.

Types of Broadcasts Receivers

  1. Dynamic Receivers -> Broadcasts messages are listened and responded to by the app only when the activity hosting the broadcast receiver is active and running. These are registered and unregistered in Activity.

  2. Static Receivers -> Broadcast messages are listened and responded to by thr app even when it is not running or is closed. The Broadcast Receivers are defined in the AndroidManifest.xml file. These Receivers are heavily limited.

Declare a Dynamic Receiver

Sure, let’s dive into the code in few steps. We will check for the whether the Airplane Mode is turned off or on.

  1. Create a broadcast receiver object. We need to override onReceive() function and then here we need to match the action received by the intent with what event we want to listen, which in this case is airplane mode change, referred by Intent.ACTION_AIRPLANE_MODE_CHANGED.

    private val isAirplaneModeOn = mutableStateOf(false)
    private val privateAirplaneModeReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                if(intent?.action == Intent.ACTION_AIRPLANE_MODE_CHANGED){
                    val isTurnedOn = Settings.Global.getInt(
                        context?.contentResolver,
                        Settings.Global.AIRPLANE_MODE_ON
                    ) != 0
    
                    isAirplaneModeOn.value = isTurnedOn
                }
            }
        }
  2. Register the broadcast in the Activity. Do that in onCreate() function. The registerReceiver() function will take broadcast object, with the intent filter.

    registerReceiver(privateAirplaneModeReceiver, IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED))
  3. Now based on the isAirplaneModeOn variable, we can perform various tasks. For demo purpose, I just have a text and icon change on the toggle.

    Row {
        if(isAirplaneModeOn.value){
             Icon(imageVector = Icons.Default.Clear, contentDescription = "Airplane Mode Enabled", tint = MaterialTheme.colorScheme.error)
             Text("Network Not Available. Airplane Mode Enabled")
        }else{
             Icon(imageVector = Icons.Default.Check, contentDescription = "Airplane Mode Disabled", tint = MaterialTheme.colorScheme.primary)
            Text("Network Available. Airplane Mode Disabled")
        }
    }
  4. Also Unregister the receiver when activity is being closed. Do this in onDestroy() function.

        override fun onDestroy() {
            super.onDestroy()
            unregisterReceiver(privateAirplaneModeReceiver)
        }

Declare Static Receiver

Now, taking same example.

  1. First create a new class MyReceiver which will contain the code that need to run when this broadcast is received.

    class AirplaneModeReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            val isAirplaneModeOn = intent?.getBooleanExtra("state", false) ?: return
            Toast.makeText(
                context,
                if (isAirplaneModeOn) "Airplane Mode ON" else "Airplane Mode OFF",
                Toast.LENGTH_SHORT
            ).show()
        }
    }
  2. Go to AndroidManifest.xml files and add receiver with intent-filter defining what type of event would you like to listen.

    <receiver android:name=".AirplaneModeReceiver" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.AIRPLANE_MODE" />
        </intent-filter>
    </receiver>

Send Broadcast from APP

Android provides two ways for apps to send broadcasts:

  1. The sendOrderedBroadcast(Intent, String) method sends broadcasts to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver. It can also completely abort the broadcast so that it doesn’t reach other receivers. You can control the order in which receivers run. To do so, use the android:priority attribute of the matching intent-filter. Receivers with the same priority are run in an arbitrary order.

  2. The sendBroadcast(Intent) method sends broadcasts to all receivers in an undefined order. This is called a Normal Broadcast. This is more efficient, but means that receivers cannot read results from other receivers, propagate data received from the broadcast, or abort the broadcast.

    val intent = Intent("com.example.snippets.ACTION_UPDATE_DATA").apply {
        putExtra("com.example.snippets.DATA", newData)
        setPackage("com.example.snippets")
    }
    context.sendBroadcast(intent)

Hence, this was everything you need to learn in order to know about the BroadCast Receivers in Android.

Official Docs: BroadCast Receivers