Listening for Broadcasts with Broadcast Receivers

Broadcast Receivers are used to listen for broadcast Intents. For a Broadcast Receiver to be needs to be registered, either in code or within the application manifest. When registering a Receiver you must use an Intent Filter to specify which Intents it is listening for.

enabled it Broadcast

To create a new Broadcast Receiver, extend the BroadcastReceiver class and override the onReceive event handler as shown in Listing 5-18.

LISTING 5-18: Broadcast Receiver skeleton implementation

Available for download on import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver { ©Override public void onReceive(Context context, Intent intent) { //TODO: React to the Intent received.

The onReceive method will be executed when a broadcast Intent is received that matches the Intent Filter used to register the Receiver. The onReceive handler must complete within five seconds or the Force Close dialog will be displayed.

Applications with Broadcast Receivers registered in the manifest don't have to be running when the Intent is broadcast for the receivers to execute. They will be started automatically when a matching Intent is broadcast. This is excellent for resource management as it lets you create event-driven applications that will still respond to broadcast events even after they've been closed or killed.

Typically Broadcast Receivers will update content, launch Services, update Activity UI, or notify the user using the Notification Manager. The five-second execution limit ensures that major processing cannot, and should not, be done within the Broadcast Receiver itself.

Listing 5-19 shows how to implement a Broadcast Receiver. In the following sections you will learn how to register it in code or in your application manifest.

LISTING 5-19: Implementing a Broadcast Receiver

. . . public class LifeformDetectedBroadcastReceiver extends BroadcastReceiver { download on 1 public static final String BURN = "com.paad.alien.action.BURN_IT_WITH_FIRE"; ©Override public void onReceive(Context context, Intent intent) { // Get the lifeform details from the intent. Uri data = intent.getData(); String type = intent.getStringExtra("type"); double lat = intent.getDoubleExtra("latitude", 0); double lng = intent.getDoubleExtra("longitude", 0); Location loc = new Location("gps"); loc.setLatitude(lat); loc.setLongitude(lng);


LISTING 5-19 (continued)

Intent startIntent = new Intent(BURN, data); startIntent.putExtra("latitude", lat); startIntent.putExtra("longitude", lng);


Registering Broadcast Receivers in Your Application Manifest

To include a Broadcast Receiver in the application manifest, add a <receiver> tag within the application node, specifying the class name of the Broadcast Receiver to register. The receiver node needs to include an intent-filter tag that specifies the action string being listened for, as shown in Listing 5-20.

^^ LISTING 5-20: Registering a Broadcast Reveiver in XML Available for downloadon <receiver android:name=".LifeformDetectedBroadcastReceiver"> <intent-filter>

<action android:name="com.paad.action.NEW_LIFEFORM"/> </intent-filter> </receiver>

Broadcast Receivers registered this way are always active, and will receive broadcast Intents even when the application has been killed or hasn't been started.

Registering Broadcast Receivers in Code

You can also register Broadcast Receivers in code. A receiver registered programmatically will respond to broadcast Intents only when the application component it is registered within is running.

This is typically useful when the Receiver is being used to update UI elements in an Activity. In this case it's good practice to un-register the Broadcast Receiver when the Activity isn't visible (or active).

Listing 5-21 shows how to register a Broadcast Receiver in code using the IntentFilter class.

LISTING 5-21: Registering a Broadcast Receiver in code

Available for downloadon // Create and register the broadcast receiver. IntentFilter filter = new IntentFilter(NEW_LIFEFORM_DETECTED);

LifeformDetectedBroadcastReceiver r = new LifeformDetectedBroadcastReceiver(); registerReceiver(r, filter);

To un-register a Broadcast Receiver use the unregisterReceiver method on your application context, passing in a Broadcast Receiver instance as follows:


Further examples can also be found in Chapter 9, where you learn to create your own background Services and use Intents to broadcast events to your Activities.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


  • baldovino
    How to implement application receiver in android?
    8 years ago
  • Edwin
    How to using longitude and latitude in broadcast receiver?
    7 years ago

Post a comment