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.

How To Broadcast an Event

// 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

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:

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.

Can we as normal developer send system-level events?

No, your application cannot broadcast the system-level events on its own. These events are triggered by the system privilege.

Priority of Receivers

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: