DEV Community

vmodal_ai
vmodal_ai

Posted on

Building Your First Android Broadcast Receiver in Kotlin

Building Your First Android Broadcast Receiver in Kotlin

Broadcast Receivers are one of Android's core components. They allow your application to listen for system-wide events such as changes in network connectivity, battery status, airplane mode, and much more.

In this tutorial, you'll learn how to create and register a simple BroadcastReceiver in Kotlin.

What is a BroadcastReceiver?

A BroadcastReceiver is a component that responds to broadcast messages from either the Android system or other applications.

Some common broadcasts include:

  • Battery charging status
  • Network connectivity changes
  • Device boot completed
  • Screen on/off
  • Airplane mode changes

Step 1: Create a BroadcastReceiver

Create a new Kotlin class.

class AirplaneModeReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        Toast.makeText(
            context,
            "Airplane Mode Changed!",
            Toast.LENGTH_SHORT
        ).show()
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Register the Receiver

Inside your Activity:

private val receiver = AirplaneModeReceiver()

override fun onStart() {
    super.onStart()

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

override fun onStop() {
    super.onStop()
    unregisterReceiver(receiver)
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Test the App

Run your application and toggle Airplane Mode.

Every time the state changes, you'll see a Toast message.

Why Register Dynamically?

Dynamic registration allows the receiver to exist only while the Activity is active.

Benefits include:

  • Lower memory usage
  • Better lifecycle management
  • Improved security
  • Avoids unnecessary background work

Best Practices

  • Always unregister dynamically registered receivers.
  • Keep onReceive() lightweight.
  • Perform long-running work using WorkManager.
  • Register receivers only when needed.

Conclusion

Broadcast Receivers are an essential Android component that enables apps to react to system events efficiently. By registering receivers dynamically and following Android lifecycle best practices, you can build responsive and efficient applications.

Thanks for reading! If you found this tutorial useful, consider following me for more Android and Kotlin development articles.

Top comments (2)

Collapse
 
marcusykim profile image
Marcus Kim

Using IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED) in onStart() with registerReceiver and balancing it in onStop() with unregisterReceiver is the key production-safe move here-I've seen too many beginner examples miss that and quietly leak listeners over time. I also like the concrete baseline you gave with AirplaneModeReceiver: BroadcastReceiver and the onReceive toast text 'Airplane Mode Changed!', which makes intent handling instantly testable. The main tradeoff is that dynamic registration is scoped to the activity lifecycle, so you lose visibility when the UI is not foregrounded but gain memory and lifecycle sanity.

Collapse
 
vmodal_ai profile image
vmodal_ai

Absolutely agree. Lifecycle-aware registration is an important detail that is easy to overlook, especially in beginner Android examples. Dynamic registration gives us a good balance between receiving events when the Activity is active and avoiding unnecessary memory leaks. Thanks for highlighting that tradeoff!