Broadcast Receiver in Android  – In our series of Android articles, Previously we discussed about Services , Activity and Fragment

Take a quick Glance at : Top 25 Android Interview Questions and Answers

In this tutorial,we’ll discuss about a important component of the Android Framework named Broadcast Receiver

What is Broadcast Receiver ?

A Broadcast Receiver is a component that responds to system-wide broadcast announcements.These messages are sometime called events or intents.
Example: Battery is getting low.

Event Constant & Description

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.BOOT_COMPLETED : This is broadcast once, after the system has finished booting

android.intent.action.BATTERY_LOW : Indicates low battery condition on the device.

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)

Two important ways  to make BroadcastReceiver works for the system broadcast intents

  • Creating the Broadcast Receiver.
  • Registering Broadcast Receiver

In first way register receiver when you feel, now you have to send broad cast message and then
unregistered when task completed(As its very battery consuming process)
In second declare in Manifest file and then no need to register. In that case life cycle of receiver will
be from when your application is installed and until your application uninstalled

Creating the Broadcast Receiver

BroadcastReceiver is an abstract class with the onReceive() method. The onReceive()method is fired
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 using
context.startActivity(myIntent) respectively.

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

}

Getting Started from Here

Registering Broadcast Receiver

An application listens for specific broadcast intents by registering a broadcast receiver in
AndroidManifest.xml file. If we are going to register MyReceiver class for system to generate the
event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has
completed the boot process.

A BroadcastReceiver can be registered in two ways:

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

<receiver android:name=”.ConnectionReceiver” >

<intent-filter>

<action android:name=”android.net.conn.CONNECTIVITY_CHANGE” />

</intent-filter>

</receiver>

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

By defining it programmatically

Following snippet shows a sample example.

IntentFilter filter = new IntentFilter();
intentFilter.addAction(getPackageName() +
“android.net.conn.CONNECTIVITY_CHANGE”);

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

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

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

Benefits of Broadcast Receiver

  • A Broadcast receiver wakes your application up, the inline code works only when your
    application is running.
  • No UI but can start an Activity
  • It has maximum limit of 10secs, do not do any asynchronous operations which may take
    more time, do not do heavy database operations or networking operations in broadcast
    receiver.

Check out : Advanced Android Topics

Also Read : Skills and Responsibilities you need to be an Android Devleoper