Android Oreo Implicit and Explicit Broadcast Receiver With Examples

In this tutorial, we’ll discuss the changes in Broadcast Receiver since Android Oreo. We’ll see why the restrictions have been put on Background Operations in Android.

Androd Broadcast Receivers

Broadcast Receivers are like Antennas. Just like Antennas can be tuned to catch certain frequencies, broadcast receivers can be registered to certain intent actions. When that action event triggers, the BroadcastReceiver gets called.

Implicit vs Explicit BroadcastReceivers

Before we differentiate, let’s know the difference between Implicit and Explicit Intents.

Explicit Intents are used to call a particular component that you know of.
Implicit Intents are used when you don’t know the exact component to invoke.

Take the example of capturing a photo from camera or gallery. It’s an implicit intent, since calling the intent shows you a dialog of different applications to choose from. Same goes for email/browser intents (if they show you a dialog chooser).

Implicit Broadcast Receivers aren’t exclusive to your application.
Actions such as ACTION_BOOT_COMPLETED or CONNECTIVITY_CHANGE are categorised in implicit broadcast receivers. This is because when these events happen, all the applications registered with the event will get the information.

Explicit Broadcast Receivers are exclusive to your application. Only your application’s broadcast receiver will get triggered when the custom intent action you define, gets called.

Handling Android Oreo Broadcast Receivers

Now, broadcast receivers run in the background. Hence it drains the battery. Imagine every application has registered android.net.conn.CONNECTIVITY_CHANGE in the receiver which is implicit.

Now, whenever the wifi/mobile internet connection toggles, all the applications, with the receiver would get triggered. Imagine what happens when your wifi router is unstable. It can cause battery issues.

Since Android Oreo, implicit broadcast receivers won’t work when registered in the AndroidManifest.xml

How to handle Implicit Receivers in Android Oreo?

To use Implicit Receivers in your application, you need to define them programmatically in your code, using registerReceiver().

Using registerReceiver() we can programmatically register and unregisterReceiver() during the lifecycle of the activity. This way implicit receivers would only be called when our activity/application is alive and not at other times.

Besides registering receivers programmatically, we can use JobSchedulers too.

Several Implicit Broadcasts are exempted and can be declared in the Manifest:

android-implicit-broadcast-exceptions

Note: Beginning with Android Pie, The NETWORK_STATE_CHANGED_ACTION broadcast doesn’t receive information about the user’s location or personally identifiable data.

In the next section, we’ll be creating an application which implements Broadcast Receivers keeping the Android Oreo limitations.

Project Structure

android-oreo-broadcast-receivers-project

Code

The AndroidManifest.xml file looks like the following:

We’ve defined an intent filter with an action which is explicit.

Implicit ones will be defined in the code only:

The code for the MainActivity.java class is given below:

On button click sendBroadcast() is called to send the explicit broadcast.

Note: sendStickyBroadcasts() were used to send broadcast intents that stayed around for other receivers to access them at a later point. The method was deprecated in API 21.

The code for the MyReceiver.java BroadcastReceiver class is given below:

The output of the above application in action is given below:

android oreo broadcast receiver output

LocalBroadcastManager

A LocalBroadcastManager is used to send or receive events locally within the current application only.

LocalBroadcastManager does not listen to system-wide broadcasts and is more secure than BroadcastReceiver.
Plus it has a less overhead hence communication speed is faster.

It can be initialised as:

LocalBroadcastManager would fail if you make events that are system wide.

We can secure broadcast receivers by setting permissions in them. Only receivers that have requested the permissions in the Manifest can receive the broadcasts.


Set the android:exported attribute to “false” in the receiver tag in the manifest restricts the application from receiving broadcasts from other applications.

This brings an end to this tutorial. You can download the project from the below link:

By admin

Leave a Reply