YouTip LogoYouTip

Android Broadcast Receivers

Broadcast receivers are used to respond to broadcast messages from other applications or the system. These messages are sometimes referred to as events or intents. For example, an application can initialize a broadcast to let other applications know that some data has been downloaded to the device and is available for them to use. Thus, broadcast receivers can define appropriate actions to intercept these communications. There are two important steps to make the system's broadcast intents work with broadcast receivers: * Create a broadcast receiver * Register the broadcast receiver There is an additional step: to implement custom intents, you must create and broadcast these intents. * * * ## Creating Broadcast Receivers A broadcast receiver needs to be implemented as a subclass of the BroadcastReceiver class and override the onReceive() method to receive messages passed as Intent objects. public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show(); }} * * * ## Registering Broadcast Receivers An application listens for specific broadcast intents by registering a broadcast receiver in the AndroidManifest.xml file. Let's assume we are going to register MyReceiver to listen for the system-generated ACTION_BOOT_COMPLETED event. This event is fired when the Android system's boot process is completed. ![Image 1: Broadcast Receivers](#) Now, whenever the Android device is booted, it will be intercepted by the broadcast receiver MyReceiver, and the logic implemented in onReceive() will be executed. There are many system-generated events defined as static constant values in the Intent class. The following table lists the important system events. | Event Constant | Description | | --- | --- | | android.intent.action.BATTERY_CHANGED | Persistent broadcast, contains battery charging status, level, and other information. | | android.intent.action.BATTERY_LOW | Indicates a low battery condition on the device. | | android.intent.action.BATTERY_OKAY | Indicates the battery is now okay after being low. | | android.intent.action.BOOT_COMPLETED | Broadcast once after the system has finished booting. | | android.intent.action.BUG_REPORT | Shows activity for bug reporting. | | android.intent.action.CALL | Executes a call data specified by someone. | | android.intent.action.CALL_BUTTON | User clicks the "Call" button to open the dialer or other appropriate interface for dialing. | | android.intent.action.DATE_CHANGED | The date has changed. | | android.intent.action.REBOOT | Device reboot. | * * * ## Broadcasting Custom Intents If you want to generate and send custom intents within your application, you need to create and send these intents using sendBroadcast() in an activity class. If you use the sendStickyBroadcast(Intent) method, the intent is sticky, meaning the intent you sent remains after the broadcast is completed. public void broadcastIntent(View view){ Intent intent = new Intent(); intent.setAction("com..CUSTOM_INTENT"); sendBroadcast(intent);} The com..CUSTOM_INTENT intent can be registered just like we registered the system-generated intents earlier. * * * ## Example This example will explain how to create a broadcast receiver to intercept custom intents. Once you are familiar with custom intents, you can program your application to intercept system-generated intents. Let's follow the steps below to modify the Android application we created in the Hello World example chapter. | Step | Description | | --- | --- | | 1 | Use Android Studio to create an Android application and name it broadcastreceiver, under the package com..broadcastreceiver as explained in the Hello World example chapter. | | 2 | Modify the main activity file MainActivity.java to add the broadcastIntent() method. | | 3 | Create a new Java file named MyReceiver.java under the com..broadcastreceiver package to define the broadcast receiver. | | 4 | An application can handle one or more custom or system intents without any restrictions. Each intent you want to intercept needs to be registered in the AndroidManifest.xml using the tag. | | 5 | Modify the default content of the res/layout/activity_main.xml file to include a button for broadcasting an intent. | | 6 | There is no need to modify the string file, Android Studio takes care of the string.xml file. | | 7 | Launch the Android emulator to run the application and verify the results of the changes made to the application. | Below is the content of the modified main activity file src/com..broadcastreceiver/MainActivity.java. This file contains each of the basic lifecycle methods. We have added the broadcastIntent() method to broadcast a custom event. package com..broadcastreceiver;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.content.Intent;import android.view.View;public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } // Broadcast a custom intent public void broadcastIntent(View view){ Intent intent = new Intent(); intent.setAction("cn.programmer.CUSTOM_INTENT"); sendBroadcast(intent); }} Below is the content of src/com..broadcastreceiver/MyReceiver.java: package com..broadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show(); }} Next, modify the AndroidManifest.xml file. Here, we include our broadcast receiver by adding the tag: Below is the content of the res/layout/activity_main.xml file, which includes a button for broadcasting a custom intent.
← Android FragmentGo Loops β†’