Effective Event Handling in Android with BroadcastReceivers

Android offers a variety of components that enable the development of robust and versatile applications. One of these important components is the BroadcastReceiver. Today, we’ll take a closer look at the Android BroadcastReceiver and demonstrate how it’s implemented in an Android application.

What is an Android BroadcastReceiver?

The Android BroadcastReceiver is a dormant component of the Android operating system that listens for system-wide broadcast events or Intents. When one of these events occurs, the application is activated by either creating a status bar notification or performing a task. Unlike activities, the Android BroadcastReceiver does not contain a user interface. It’s typically implemented to delegate tasks to services depending on the type of Intent data received.

Key System-Wide Generated Intents

Some key system-wide generated intents are:

  • android.intent.action.BATTERY_LOW: Indicates that the device is in low battery state.
  • android.intent.action.BOOT_COMPLETED: This is broadcast once after the system has finished booting.
  • android.intent.action.CALL: To make a call to a specific person.
  • android.intent.action.DATE_CHANGED: The date has changed.
  • android.intent.action.REBOOT: Reboot the device.
  • android.net.conn.CONNECTIVITY_CHANGE: The mobile network or Wi-Fi connection has changed (or reset).

Implementing a BroadcastReceiver in Android

To set up a BroadcastReceiver in an Android application, two steps need to be performed:

  1. Creating a BroadcastReceiver
  2. Registering a BroadcastReceiver

Creating a BroadcastReceiver

A custom BroadcastReceiver can be implemented as follows:

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }
 
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();
    }
}

The BroadcastReceiver is an abstract class, whose method onReceive() is abstract. This method is called first on the registered broadcast receivers when an event occurs. The Intent object is passed along with any additional data. A Context object is also available and used to start an activity or service.

Registering the BroadcastReceiver in the Android App

A BroadcastReceiver can be registered in two ways:

  1. By defining in the AndroidManifest.xml file

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

  1. Programmatically

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

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

Sending Broadcast Intents from the Activity

Intent intent = new Intent();
intent.setAction("com.journaldev.CUSTOM_INTENT");
sendBroadcast(intent);

Summary

The BroadcastReceiver is an important component of Android that allows responding to system-wide events. By creating and registering BroadcastReceivers, Android applications can effectively respond to various events and perform appropriate actions.

That was a brief introduction and tutorial on the Android BroadcastReceiver. Hopefully, this post has helped you better understand the concept and how it can be applied in your own Android development.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: