
Android Wear Docs, Release 1.1
4.1.2 Modify the Handheld Activity
Your handheld activity initiates the process by creating a notification that includes an action (demand). When a user
views the notification and invokes the demand, the wearable broadcasts the demand to the handheld for processing.
You build the notification with a hierarchy of objects, intent -> pending intent -> notification action -> wearable
extender, and finally the notification itself.
1. Create an Intent that defines the action that the handheld device should take in response to a wearable demand.
The Intent has the following parameters:
> The context of the activity, service, or broadcast receiver on the handheld that handles wearable demands.
> The name of the class on the handheld that receives wearable demands.
> A constant that specifies a demand action.
> An extra with details about the requested action.
The following example shows how to create an intent for a reply demand. The DemandIntentReceiver is defined
later in Create a Demand Receiver.
public static final String ACTION_DEMAND = "com.androidweardocs.ACTION_DEMAND";
public static final String EXTRA_MESSAGE = "com.androidweardocs.EXTRA_MESSAGE";
public static final String EXTRA_VOICE_REPLY = "com.androidweardocs.EXTRA_VOICE_REPLY";
Intent demandIntent = new Intent(this, DemandIntentReceiver.class)
.putExtra(EXTRA_MESSAGE, “Reply selected.")
.setAction(ACTION_DEMAND);
2. Create a PendingIntent to include in the notification.
A PendingIntent wraps the intent to grant the privileges it needs for to execute in your application. It
contains the context of the activity, service, or broadcast receiver that will receive the demand, and the
Intent object itself. This example creates a PendingIntent using the context of a broadcast receiver. Use
getActivity instead of getBroadcast if your activity receives demands.
PendingIntent demandPendingIntent =
PendingIntent.getBroadcast(this, 0, demandIntent, 0);
3. Create a RemoteInput object to hold a voice reply from the wearable device. A voice request or response is a
common action for a wearable device because of the small size of the UI.
String replyLabel = getResources().getString(R.string.app_name);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.build();
4. Create a wearable action.
The following example creates an wearable action that uses a standard reply icon and label, adds the
pending intent, and the RemoteInput object for voice.
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon,
getString(R.string.reply_label), demandPendingIntent)
.addRemoteInput(remoteInput)
.build();
5. Create a WearableExtender for the a notification and add the wearable action.
18 Chapter 4. Android Wear Demand
Comentarios a estos manuales