Broadcast Receivers in Android

Deepak Kaligotla
3 min readSep 2, 2023

--

There are two types of Android Broadcast Receivers:

Static Broadcast Receiver:

A static broadcast receiver is defined in the app’s manifest file and can respond to system-wide events even when the app is not running. Static receivers are registered in the manifest file with an intent filter that specifies the type of event they can handle. Some examples of static broadcast receivers include:

  • Boot Completed Receiver: As mentioned earlier, a boot completed receiver listens for the “ACTION_BOOT_COMPLETED” system broadcast, which is triggered when the device finishes booting up. This receiver can be defined in the app’s manifest file, and it will run even if the app is not running.
  • Package Added/Removed Receiver: A package added/removed receiver listens for the “android.intent.action.PACKAGE_ADDED” and “android.intent.action.PACKAGE_REMOVED” system broadcasts, which are triggered when an app is installed or uninstalled on the device. This receiver can be defined in the app’s manifest file, and it can perform actions such as updating UI, cleaning up resources, or sending notifications.

Step -1 : create a new Java class that extends the BroadcastReceiver class:

public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do something when the broadcast is received
}
}

Step 2 : add a new <receiver> tag to your AndroidManifest.xml file, specifying the name of your receiver class and the action that it should listen for:

<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="com.example.myapp.ACTION_MY_BROADCAST" />
</intent-filter>
</receiver>

Step 3 : Finally, you can send the broadcast from anywhere in your app using an Intent object with the specified action:

Intent intent = new Intent("com.example.myapp.ACTION_MY_BROADCAST");
sendBroadcast(intent);

When the broadcast is sent, the onReceive() method in your MyReceiver class will be called, allowing you to perform any necessary actions in response to the broadcast.

Dynamic Broadcast Receiver:

A dynamic broadcast receiver is registered and unregistered at runtime, and can only respond to events while the app is running. Dynamic receivers are typically used for events that are specific to the app, such as events triggered by user actions or data changes. Some examples of dynamic broadcast receivers include:

  • Button Click Receiver: A button click receiver listens for button click events and responds by performing an action, such as starting a new activity or updating the UI. This receiver is registered and unregistered at runtime, and it can only respond to events while the app is running.
  • Location Change Receiver: A location change receiver listens for the “android.location.PROVIDERS_CHANGED” system broadcast, which is triggered when the device’s location provider settings are changed. This receiver can be registered and unregistered at runtime, and it can respond by performing an action, such as starting a location update service or updating the UI with the new location.

Step 1 : create a new Java class that extends the BroadcastReceiver class

public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do something when the broadcast is received
}
}

Step 2 : in your activity or service, create an instance of your receiver class and register it with the system using an IntentFilter

MyReceiver myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.myapp.ACTION_MY_BROADCAST");
registerReceiver(myReceiver, intentFilter);

Note: This will register your receiver to listen for broadcasts with the specified action. Don’t forget to unregister the receiver when you’re done using it to avoid memory

unregisterReceiver(myReceiver);

Step 3 : you can send the broadcast from anywhere in your app using an Intent object with the specified action

Intent intent = new Intent("com.example.myapp.ACTION_MY_BROADCAST");
sendBroadcast(intent);

When the broadcast is sent, the onReceive() method in your MyReceiver class will be called, allowing you to perform any necessary actions in response to the broadcast.

In summary, static broadcast receivers are defined in the app’s manifest file and can respond to system-wide events even when the app is not running, while dynamic broadcast receivers are registered and unregistered at runtime and can only respond to events while the app is running. Both types of receivers can be useful for different types of events and use cases.

--

--

Deepak Kaligotla

I’m jobless, have 5.5 yrs experience in IT Tech support/helpdesk. Love learning and developing Mobile applications (Android & iOs). Contact me if you can hire