original source :http://www.androiddocs.com/training/wearables/notifications/voice-input.html

Receiving Voice Input in a Notification

If you have handheld notifications that include an action to input text, such as reply to an email, it should normally launch an activity on the handheld device to input the text. However, when your notification appears on a wearable, there is no keyboard input, so you can let users dictate a reply or provide pre-defined text messages using RemoteInput.

When users reply with voice or select one of the available messages, the system attaches the text response to the Intentyou specified for the notification action and sends that intent to your handheld app.

Define the Voice Input

To create an action that supports voice input, create an instance of


that you can add to your notification action. This class’s constructor accepts a string that the system uses as the key for the voice input, which you’ll later use to retrieve the text of the input in your handheld app.For example, here’s how to create a


object that provides a custom label for the voice input prompt:

// Key for the string that's delivered in the action's intent
private static final String EXTRA_VOICE_REPLY = "extra_voice_reply";

String replyLabel = getResources().getString(R.string.reply_label);

RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)

Add Pre-defined Text Responses



<?xml version="1.0" encoding="utf-8"?>
   <string-array name="reply_choices">
public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
String replyLabel = getResources().getString(R.string.reply_label);
String[] replyChoices = getResources().getStringArray(R.array.reply_choices);

RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)

Add the Voice Input as a Notification Action

// Create an intent for the reply action
Intent replyIntent = new Intent(this, ReplyActivity.class);
PendingIntent replyPendingIntent =
       PendingIntent.getActivity(this, 0, replyIntent,

// Create the reply action and add the remote input
NotificationCompat.Action action =
       new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon,
               getString(R.string.label), replyPendingIntent)

// Build the notification and add the action via WearableExtender
Notification notification =
       new NotificationCompat.Builder(mContext)
               .extend(new WearableExtender().addAction(action))

// Issue the notification
NotificationManagerCompat notificationManager =
notificationManager.notify(notificationId, notification);

Receiving the Voice Input as a String

(notification은 sync와는 달리 일방통행이라고 하지만 reply action intent를 통해 reply message를 전달 받을수 있다. )

To receive the user’s transcribed message in the activity you declared in the reply action’s intent, call


, passing in the “Reply” action’s intent. This method returns a


that contains the text response. You can then query the


to obtain the response.


Do not use


to obtain the voice result, because the voice input is stored as


. The


method provides a convenient way to receive a character sequence without having to process the



* Obtain the intent that started this activity by calling
* Activity.getIntent() and pass it into this method to
* get the associated voice input string.

private CharSequence getMessageText(Intent intent) {
   Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
   if (remoteInput != null) {
       return remoteInput.getCharSequence(EXTRA_VOICE_REPLY);
   return null;

Comments are closed.

Post Navigation