Android BroadcastReceiver

person shubham sharmafolder_openAndroidlocal_offer, access_time March 11, 2017

In this tutorial we’ll discuss and implement a very important component of the Android Framework named BroadcastReceiver.

Android BroadcastReceiver Overview

A BroadcastReceiver is a dormant component of Android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task. Unlike activities, a BroadcastReceiver doesn’t contain any user interface. It’s generally implemented to delegate the tasks to services depending on the type of intent data that’s received. Following are some of the important system wide generated intents.

  • android.intent.action.BATTERY_LOW : Indicates low battery condition on the device.
  • android.intent.action.BOOT_COMPLETED : This is broadcast once, after the system has finished booting
  • android.intent.action.CALL : To perform a call to someone specified by the data
  • android.intent.action.DATE_CHANGED : The date has changed
  • android.intent.action.REBOOT : Have the device reboot
  • android.net.conn.CONNECTIVITY_CHANGE : The mobile network or wifi connection is changed(or reset)

To set up a Broadcast Receiver in our android application we need to do the following two things.

  • Creating a BroadcastReceiver
  • Registering a BroadcastReceiver

Creating a BroadcastReceiver

Let’s quickly implement a custom BroadcastReceiver as shown below:

BroadcastReceiver is an abstract class with the onReceiver() method being abstract. The onReceiver()method is first called on the registered BroadcastReceivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.startActivity(myIntent); or context.startService(myService); respectively.

Registering the BroadcastReceiver

A BroadcastReceiver can be registered in two ways:

By defining it in the AndroidManifest.xml file as shown below.

 

Using intent filters we tell the system any intent that matches our subelements should get delivered to that specific broadcast receiver.

 

  • By defining it programmatically

 

Following snippet shows a sample example.

To unregister a receiver in onStop() or onPause() of the activity the following snippet can be used.

Sending Broadcast intents from the Activity

The following snippet is used to send an intent to all the related BroadcastReceivers.

 

Don’t forget to add the above action in the intent filter tag of the manifest or programmatically.

Let’s develop an application that listens to network change events and also to a custom intent and handles the data accordingly.

Project Structure

android-broadcast-receiver-project

Code

The activity_main.xml consists of a button at the center that sends a broadcast intent.

activity_main.xml

The MainActivity.java is given below.

MainActivity.java

In the above code we’ve registered another custom action programmatically.

The ConnectionReceiver is defined in the AndroidManifest.xml file as below:

AndroidManifest.xml

The ConnectionReceiver.java class is defined below.

In the above code we check the intent action that triggers the onReceive() method and based on that display the toast.

Note: To make the receiver unavailable to external applications, add the attribute android:exported=falsein the manifest. When we send a broadcast, it is possible for the external applications too to receive them. This can be prevented by specifying this limitation.

The output app in action is given below.
android-broadcast-receiver-output

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

BroadcastReceiver

warningComments are closed.