Junior — Middle
Can you name different types of Broadcast Receivers used in Android?
sobes.tech AI
Answer from AI
In Android, there are two main types of Broadcast Receivers:
-
Static Broadcast Receivers — declared in the application's manifest (
AndroidManifest.xml). They can receive broadcast messages even when the app is not running. However, from Android 8.0 (API 26) onwards, their use is limited for certain system events to save resources. -
Dynamic Broadcast Receivers — registered programmatically in code (usually in
onCreateoronResumeof an activity) usingregisterReceiver(). They are active only while the app or component is running and registered.
Additionally, you can distinguish:
- Ordered Broadcast Receivers — receive messages sequentially, with the ability to abort further propagation.
- Local Broadcast Receivers — operate within a single app, not leaving its boundaries, which enhances security and performance (using
LocalBroadcastManager).
Example of dynamic registration:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// handle event
}
};
registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));