Broadcast Receivers in Android
A Broadcast Receiver is a component in Android used to listen for system-wide or app-specific broadcast messages (or events). These messages can be sent by the system (e.g., when the device boots) or by an application.
Types of Broadcasts
- System Broadcasts: Sent by the Android system (e.g., battery level changes, connectivity changes, or incoming calls).
- Custom Broadcasts: Sent by applications to communicate events within the app or to other apps.
How Broadcast Receivers Work
- Intent: Broadcast messages are sent using an
Intent
. - Receiver: A Broadcast Receiver listens for these intents and executes code when the intent is received.
Declaring a Broadcast Receiver
- Static Receiver (Manifest Declaration)
Used for system-wide broadcasts or events. Declared in theAndroidManifest.xml
. - Dynamic Receiver (Code Registration)
Registered at runtime, often used for app-specific needs. Declared usingContext.registerReceiver
.
Steps to Implement a Broadcast Receiver
1. Create the Receiver Class
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// Handle the broadcast
if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {
Toast.makeText(context, "Device Booted", Toast.LENGTH_SHORT).show()
}
}
}
2. Static Registration in Manifest
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
- Add the required permission in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
3. Dynamic Registration
class MainActivity : AppCompatActivity() {
private lateinit var myReceiver: MyBroadcastReceiver
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myReceiver = MyBroadcastReceiver()
// Register the receiver dynamically
val intentFilter = IntentFilter().apply {
addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED)
}
registerReceiver(myReceiver, intentFilter)
}
override fun onDestroy() {
super.onDestroy()
// Unregister the receiver
unregisterReceiver(myReceiver)
}
}
Broadcast Sending
To send a custom broadcast:
Send a Broadcast
val intent = Intent("com.example.MY_CUSTOM_ACTION")
intent.putExtra("key", "value")
sendBroadcast(intent)
Receive a Custom Broadcast
Add a filter for the custom action in your receiver:
val intentFilter = IntentFilter("com.example.MY_CUSTOM_ACTION")
registerReceiver(myReceiver, intentFilter)
Common System Broadcast Actions
Action | Description |
---|---|
Intent.ACTION_BOOT_COMPLETED | Triggered when the device boots up. |
Intent.ACTION_BATTERY_LOW | Triggered when the battery is low. |
Intent.ACTION_AIRPLANE_MODE_CHANGED | Triggered when airplane mode changes. |
Intent.ACTION_POWER_CONNECTED | Triggered when power is connected. |
Intent.ACTION_POWER_DISCONNECTED | Triggered when power is disconnected. |
Best Practices
- Static vs Dynamic Registration: Use static registration for system-level events and dynamic registration for app-specific needs.
- Security: Restrict broadcasts using permissions to prevent misuse by other apps.
- Performance: Avoid long-running operations in
onReceive
as it runs on the main thread. Use a service if needed.