What Are Android Broadcasts?
Android broadcasts are asynchronous messages used for communication between different parts of the system or application components. These messages notify components when specific events occur, either within the app or system-wide.
Why and When Broadcasts Are Needed?
Broadcasts are needed to signal events such as system boot, network connectivity change, low battery, or custom application events. They allow communication without direct coupling between components.
Types of Android Broadcasts
System Broadcasts
System broadcasts are sent by the Android system when predefined events occur, such as device reboot, power connected, or screen turned off.
// Register in AndroidManifest.xml
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
// Receiver class
class PowerConnectionReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_POWER_CONNECTED) {
// Handle power connected event
}
}
}
Local Broadcasts
Local broadcasts are sent and received within the same application. They offer better security and performance as they do not leave the app boundary.
// Sending a local broadcast
val intent = Intent("custom-event")
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
// Receiving a local broadcast
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Handle the custom event
}
}
val filter = IntentFilter("custom-event")
LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter)
Context-Registered (Dynamic) Broadcasts
These are registered at runtime using the context object. They allow for more flexible behavior but require manual registration and unregistration.
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Handle broadcast
}
}
val filter = IntentFilter(Intent.ACTION_BATTERY_LOW)
context.registerReceiver(receiver, filter)
// Don't forget to unregister
context.unregisterReceiver(receiver)
Considerations When Using Broadcasts
- Security: Exported receivers can be invoked by other apps. Use permission declarations to restrict access.
- Performance: Excessive use of system broadcasts can impact performance, especially in background operations.
- Lifecycle: Dynamic receivers must be unregistered properly to avoid memory leaks.
Advantages of Using Broadcasts
- Loose coupling between components.
- Asynchronous communication across the system or within the app.
- Reusability through shared broadcast receivers across multiple components.
Limitations
- Cannot send large data through intents.
- Unreliable for long operations; better suited for lightweight notifications.
- Alternatives include using interfaces, LiveData (for in-app), WorkManager (for background tasks), or shared ViewModel (in MVVM architecture).