Broadcast Receivers
Broadcast Receivers:
BroadCastReceiver, is a component that helps the system to convey an event or message on behalf of the application in the background.
BroadcastReceivers are the right tool to handle extrensive system surprising, for instance, a new SMS message, a change of status in the network, or the battery-completed device.
Creating a BroadcastReceiver:
To create a BroadcastReceiver, you need to extend the BroadcastReceiver class and override the onReceive method. This method is called when the specified broadcast event occurs.
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Handle the received broadcast event
String action = intent.getAction();
if (action != null && action.equals("custom.action")) {
// Custom action handling
String data = intent.getStringExtra("key");
// Process data as needed
}
}
}
Registering a BroadcastReceiver:
You can register a BroadcastReceiver dynamically at runtime or declare it in the AndroidManifest.xml file to be activated automatically by system events.
Dynamic Registration (in Activity/Fragment):
MyReceiver myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter("custom.action");
registerReceiver(myReceiver, intentFilter);
Static Registration (in AndroidManifest.xml):
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="custom.action" />
</intent-filter>
</receiver>
Sending Broadcasts:
To send a broadcast from your app, create an Intent and use the sendBroadcast method.
// Sending a broadcast from within an activity or service
Intent broadcastIntent = new Intent("custom.action");
broadcastIntent.putExtra("key", "some data");
sendBroadcast(broadcastIntent);
BroadcastReceiver Lifecycle:
In the case of a BroadcastReceiver’s onReceive callback, it is expected to execute very fast as it runs on main thread and anything lengthy or blocking will slow down the UI responsiveness. If this is the case, you could request additional time which can be done through the use of a JobIntentService or a JobService.
BroadcastReceiver Permissions:
You can specify permissions required to receive a broadcast by setting the android:permission attribute in the <receiver> tag in the manifest.
<receiver android:name=".MyReceiver" android:permission="custom.permission">
<intent-filter>
<action android:name="custom.action" />
</intent-filter>
</receiver>
Ordered Broadcasts:
Broadcasts can be ordered, allowing multiple receivers to process the broadcast in a specific order. This is achieved by setting priorities and using setResult and abortBroadcast methods.
System Broadcasts:
Android system broadcasts, such as ACTION_BOOT_COMPLETED or CONNECTIVITY_CHANGE, can be received by registering a receiver in the manifest.
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
BroadcastReceivers provide a powerful mechanism for communication within an app or between different apps.