|
Cyril Sermon (@admin) |
As a system-level message-passing mechanism, Intents are capable of sending structured messages across process boundaries.
So far you’ve looked at using Intents to start new application components, but they can also be used to broadcast messages anonymously between components with the sendBroadcast method. You can imple-ment Broadcast Receivers to listen for, and respond to, these broadcast Intents within your applications.
Broadcast Intents are used to notify listeners of system or application events, extending the event-driven programming model between applications.
Broadcasting Intents helps make your application more open; by broadcasting an event using an Intent, you let yourself and third-party developers react to events without having to modify your original application. Within your applications, you can listen for Broadcast Intents to replace or enhance native (or third-party) applications or react to system changes and application events.
For example, by listening for the incoming call broadcast, you can modify the ringtone or volume based on the caller.
Android uses Broadcast Intents extensively to broadcast system events like battery-charging levels, network connections, and incoming calls.
Broadcasting Intents is actually quite simple. Within your application component, construct the Intent you want to broadcast, and use the sendBroadcast method to send it.
Set the action, data, and category of your Intent in a way that lets Broadcast Receivers accurately deter-mine their interest. In this scenario, the Intent action string is used to identify the event being broadcast, so it should be a unique string that identifies the event. By convention, action strings are constructed using the same form as Java packages, as shown in the following snippet:
public static final String NEW_LIFEFORM_DETECTED = “com.paad.action.NEW_LIFEFORM”;
If you wish to include data within the Intent, you can specify a URI using the Intent’s data property. You can also include extras to add additional primitive values. Considered in terms of an event-driven paradigm, the extras Bundle equates to optional parameters within an event handler.
The skeleton code below shows the basic creation of a Broadcast Intent using the action defined previ-ously, with additional event information stored as extras.
Intent intent = new Intent(NEW_LIFEFORM_DETECTED); intent.putExtra(“lifeformName”, lifeformType); intent.putExtra(“longitude”, currentLongitude); intent.putExtra(“latitude”, currentLatitude);
sendBroadcast(intent);
Broadcast Receivers are used to listen for Broadcast Intents. To enable a Broadcast Receiver, it needs to be registered, either in code or within the application manifest. When registering a Broadcast Receiver, you must use an Intent Filter to specify which Intents it is listening for.
To create a new Broadcast Receiver, extend the BroadcastReceiver class and override the onReceive event handler as shown in the skeleton code below:
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 5 seconds, or the Application Unresponsive dialog will be displayed.
Applications with registered Broadcast Receivers do not have to be running when the Intent is broad-cast 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 can be closed or killed, safe in the knowledge that they will still respond to broadcast events.
Typically, Broadcast Receivers will update content, launch Services, update Activity UI, or notify the user using the Notification Manager. The 5-second execution limit ensures that major processing cannot, as it should not, be done within the Broadcast Receiver directly.
The following example 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.
public class LifeformDetectedBroadcastReceiver extends BroadcastReceiver {
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);
if (type.equals(“alien”)) {
Intent startIntent = new Intent(BURN, data);
startIntent.putExtra(“latitude”, lat);
startIntent.putExtra(“longitude”, lng);
context.startActivity(startIntent);
}
}
}