Think of a broadcast receiver like a doorbell. When someone rings the doorbell, you get a notification and can do something about it, like answering the door. Similarly, a broadcast receiver listens for certain events to happen on your Android phone, like getting a text message or losing network connectivity. When the event happens, the broadcast receiver gets a notification and can perform an action, like displaying a notification or starting a specific task.
There are two types of broadcast receivers: static and dynamic. Static receivers are declared in the app’s manifest file and can work even if the app is closed. Dynamic receivers are registered in the app’s code and only work if the app is active or minimized.
Static Receiver
<!-- AndroidManifest.xml -->
<manifest>
<application>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
</application>
</manifest>
Java code:
// MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// Perform action when device boots up
} else if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
// Perform action when device is plugged in
}
}
}
Dynamic Receiver
// MainActivity.java
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver myBroadcastReceiver;
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Perform action when event occurs
}
};
intentFilter = new IntentFilter();
intentFilter.addAction("com.myapp.EVENT");
registerReceiver(myBroadcastReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
}
}
// MyService.java
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent("com.myapp.EVENT");
sendBroadcast(intent);
}
}
System-level events are events that are triggered by the Android operating system and are not specific to any particular app. These events are typically related to the overall functioning of the device, such as changes in network connectivity, battery level, or device orientation.
Examples of system-level events in Android include:
ACTION_BOOT_COMPLETED: Triggered when the device finishes booting up.ACTION_BATTERY_LOW: Triggered when the battery level drops below a certain threshold.ACTION_SCREEN_ON and ACTION_SCREEN_OFF: Triggered when the device screen is turned on or off.ACTION_AIRPLANE_MODE_CHANGED: Triggered when the device's airplane mode is turned on or off.ACTION_TIMEZONE_CHANGED: Triggered when the device's timezone is changed.These events are broadcast by the system and can be received by any app that has registered a broadcast receiver for the corresponding event. Apps can use these events to perform actions or update their user interface based on the current state of the device.
No, your application cannot broadcast the system-level events on its own. These events are triggered by the system privilege.
In the AndroidManifest.xml file, you can set the priority of a broadcast receiver to determine the order in which multiple broadcast receivers should receive a particular broadcast event. The priority is an integer value that can range from -1000 to 1000, where a higher value indicates a higher priority. The default priority value for a broadcast receiver is 0.
Here is an example of how to set the priority of a broadcast receiver in the manifest file: