Android Broadcast Receiver

Anubhav Sharma
3 min readAug 8, 2022

--

Broadcast receiver are one of the four core android components. Simply, Broadcast Receivers can send or receive messages from other applications or from the system itself. Hello! Dear Android Developers my name is Anubhav Sharma and today, I will talk about the Broadcast Receiver. These messages can be events or intents. For instance, Android system sends broadcasts when system events occur such as system boots up, device starts charging, connectivity chaining, date chaining, low battery. Furthermore, apps can send custom broadcasts to notify other apps(data download completed).

Apps can receive broadcasts in two ways:

1-Manifest-declared receivers(Statically)

2-Context-registered receivers(Dynamically)

Manifest-declared receivers(Statically)

The registration is done in the manifest file, using <register> tags.

Firstly, specify the receiver in your manifest file.

<receiver android:name=".MyBroadcastReceiver"  android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE""/>

</intent-filter>
</receiver>

Secondly, create class as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.

public class MyBroadcastReceiver extends BroadcastReceiver {   @Override
public void onReceive
(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}

Context-registered receivers(Dynamically)

The registration is done using Context.registerReceiver() method.

Firstly, register broadcast receiver programmatically.

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(getPackageName() + "android.net.conn.CONNECTIVITY_CHANGE");

MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
registerReceiver(myReceiver, filter);

Secondly, To unregister a broadcast receiver in onStop() or onPause() of the activity.

@Override protected void onPause() {  
unregisterReceiver(myReceiver);
super.onPause(); }

Be mindful of where you register and unregister the receiver, for example, if you register a receiver in onCreate(Bundle) using the activity's context, you should unregister it in onDestroy() to prevent leaking the receiver out of the activity context. If you register a receiver in onResume(), you should unregister it in onPause() to prevent registering it multiple times (If you don't want to receive broadcasts when paused, and this can cut down on unnecessary system overhead). Do not unregister in onSaveInstanceState(Bundle), because this isn't called if the user moves back in the history stack.

Sending broadcasts

There are three distinctive way to send broadcast:

1- The sendOrderedBroadcast(Intent, String) method sends broadcasts to one receiver at a time.

2-The sendBroadcast(Intent) method sends broadcasts to all receivers in an undefined order.

3-The LocalBroadcastManager.sendBroadcast method sends broadcasts to receivers that are in the same app as the sender.

Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_NOTIFICATION");
intent.putExtra("data", "Nothing to see here, move along.");
sendBroadcast(intent);//With permissions
sendBroadcast(new Intent("com.example.NOTIFY"),
Manifest.permission.SEND_SMS);
//the receiving app must request the permission
<uses-permission android:name="android.permission.SEND_SMS"/>

List of Broadcast receiver events:

There are some static fields defined in the Intent class which can be used to broadcast different events. We have taken a change of airplane mode as a broadcast event, but there are many events for which broadcast register can be used. Following are some of the important system-wide generated intents:-

1.android.intent.action.BATTERY_LOW 
// Indicates low battery condition on the device.
2.android.intent.action.BOOT_COMPLETED
// This is broadcast once after the system has finished booting
3.android.intent.action.CALL
// To perform a call to someone specified by the data 4.android.intent.action.DATE_CHANGED
// Indicates that the date has changed
5.android.intent.action.REBOOT
// Indicates that the device has been a reboot
6.android.net.conn.CONNECTIVITY_CHANGE
//The mobile network or WIFI connection is changed(or reset)
7.android.intent.ACTION_AIRPLANE_MODE_CHANGED
//This indicates that airplane mode has been switched on or off.

I hope it was a useful article! thank you.

--

--

Anubhav Sharma

I’m a software developer specialized in native Android app development with 5+ years of experience and worked with latest technologies in trend.