Understanding Broadcast Receivers In Android
Android Broadcast receivers are an integral part and used for receiving and handling system-wide and application-wide events or broadcasts. They are registered with the system to receive broadcasts sent by the system or other applications.
Broadcast receivers are used to perform tasks when a specific event occurs in the system, such as the battery being low, the device being restarted, or a specific action being performed in the application.
Broadcast receivers are useful in various scenarios such as updating UI components, responding to system-wide events, starting or stopping services, or displaying notifications. For instance, a broadcast receiver can be used to detect when a user plugs or unplugs a headset and then takes action like pausing or resuming music playback.
To create a broadcast receiver, you need to extend the BroadcastReceiver class and implement the onReceive() method.
This method is called when the receiver receives a broadcast. In the onReceive() method, you can get the data passed in the broadcast intent and perform the desired action.
Below example illustrates how to create a broadcast receiver in Android:
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Do something when a broadcast is received } }
Types of Broadcast Receivers
There are two types of Broadcast Receivers in Android:
Static Broadcast Receiver
As its name suggests, the Static Broadcast Receiver is declared statically in AndroidManifest.xml.
The system registers and unregisters it automatically when an application is installed and uninstalled.
In order to receive system-level events, such as the battery running low or the airplane mode changed, static broadcast receivers are useful.
Because they can be registered even when an application isn’t running.
To register a receiver statically in the AndroidManifest.xml file, add the following code:
<receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcast.MY_ACTION" /> </intent-filter> </receiver>
Dynamic Broadcast Receiver
With the registerReceiver() method, Dynamic Broadcast Receiver can be registered and unregistered programmatically at runtime.
As opposed to the static Broadcast Receiver, this one is not declared in AndroidManifest.xml.
These receivers are useful when you want to receive application-level events that are generated during runtime and not available during installation.
To register a receiver dynamically through code, you can use the registerReceiver() method:
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter("com.example.broadcast.MY_ACTION"); registerReceiver(myReceiver, intentFilter);
Broadcast Receiver Lifecycle
Broadcast Receivers have a different lifecycle than other Android components, such as Activities and Services.
A Broadcast Receiver’s lifespan is limited by its onReceive() method, which is called by the system when an event occurs.
Upon completion of the onReceive() method, the Broadcast Receiver is destroyed.
Following example implements a Broadcast Receiver to listen to the battery low event:
Create a new Java class that extends BroadcastReceiver:
public class BatteryBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) { Toast.makeText(context, "Battery is low!", Toast.LENGTH_SHORT).show(); } } }
Register the Broadcast Receiver in the AndroidManifest.xml file:
<receiver android:name=".BatteryBroadcast"> <intent-filter> <action android:name="android.intent.action.BATTERY_LOW" /> </intent-filter> </receiver>
Whenever the battery level falls below a certain threshold, the system broadcasts the BATTERY_LOW event, and the registered Broadcast Receiver’s onReceive() method is called. In this example, the Broadcast Receiver displays a Toast message when the battery is low.