Login     Sign Up
Tugadar (@admin)
20 days ago
21 Views

To include a Broadcast Receiver in the application manifest, add a receiver tag within the applica-tion 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 the XML snippet below:

<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.

Registering Broadcast Receivers in Code

You can control the registration of Broadcast Receivers in code. This is typically done when the receiver is being used to update UI elements in an Activity. In this case, it’s good practice to unregister Broad-cast Receivers when the Activity isn’t visible (or active).

The following code snippet shows how to register a Broadcast Receiver using an IntentFilter:

// Create and register the broadcast receiver.

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

To unregister a Broadcast Receiver, use the unregisterReceiver method on your application context, passing in a Broadcast Receiver instance, as shown below:

unregisterReceiver(r);

Further examples can also be found in Chapter 8 when you learn to create your own background Services and use Intents to broadcast events back to your Activities.

Native Android Broadcast Actions

Android broadcasts Intents for many of the system Services. You can use these messages to add func-tionality to your own projects based on system events such as time-zone changes, data-connection sta-tus, incoming SMS messages, or phone calls.

The following list introduces some of the native actions exposed as constants in the Intents class; these actions are used primarily to track device status changes:

❑ACTION_BOOT_COMPLETED Fired once when the device has completed its start-up sequence. Requires the RECEIVE_BOOT_COMPLETED permission.

❑ACTION_CAMERA_BUTTON Fired when the Camera button is clicked.

ACTION_DATE_CHANGED and ACTION_TIME_CHANGED These actions are broadcast if the date

or time on the device is manually changed (as opposed to them changing through the natural progress of time).

ACTION_GTALK_SERVICE_CONNECTED and ACTION_GTALK_SERVICE_DISCONNECTED These

two actions are broadcast each time a GTalk connection is made or lost. More specific handling of GTalk messaging is discussed in more detail in Chapter 9.

❑ACTION_MEDIA_BUTTON Fired when the Media button is clicked.

❑ACTION_MEDIA_EJECT If the user chooses to eject the external storage media, this event is fired first. If your application is reading or writing to the external media storage, you should listen for this event in order to save and close any open file handles.

ACTION_MEDIA_MOUNTED and ACTION_MEDIA_UNMOUNTED These two events are broadcast

whenever new external storage media are successfully added or removed from the device.

ACTION_SCREEN_OFF and ACTION_SCREEN_ON Broadcasts when the screen turns off or on.

❑ACTION_TIMEZONE_CHANGED This action is broadcast whenever the phone’s current time zone changes. The Intent includes a time-zone extra that returns the ID of the new java.util.TimeZone.

A comprehensive list of the broadcast actions used and transmitted natively by Android to notify applications of system state changes is available at http://code.google.com/android/reference/ android/content/Intent.html.

Android also uses Broadcast Intents to monitor application-specific events like incoming SMS mes-sages. The actions and Intents associated with these events are discussed in more detail in later chap-ters when you learn more about the associated Services.