Android Broadcast Receiver

What is the Broadcast Receiver in Android?

Hüseyin Özkoç
3 min readNov 17, 2021
Android Broadcast Receiver

Hello! Welcome to my article Dear Android Developers! and Dear Predators!
In my article today, I will talk about the Broadcast Receiver.

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. 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, connetivity chaning, date chaning, 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"/>

I hope it was a useful article! See you in my other articles!

Photo by Charles Deluvio on Unsplash

--

--