Android Wear performs voice interaction on Wearable devices

Source: Internet
Author: User

Android Wear performs voice interaction on Wearable devices

Receive voice input in notification

If you create a notification on your mobile phone that includes a behavior, such as an action such as replying to an email, an activity usually appears for the user to input, but then in the wearable device, there is no keyboard for users to use. Therefore, RemoteInput can be used for user interaction.

When a user uses a voice reply or supports other input methods, the system binds the text reply to the Intent of the Notification action you specified, and then transmits the Intent to the app of the mobile phone.

Define Voice Input

To create a behavior that can accept voice input, you need to instantiate RemoteInput. Builder and add it to the action of the notification. Its constructor receives a string, which is the key used by the system for voice input. After that, the voice input can be associated with the app.

For example, the following code creates a RemoteInput object and provides a method for voice input.

// Key for the string that's deliveredin the action's intentprivate staticfinalString EXTRA_VOICE_REPLY="extra_voice_reply";String replyLabel = getResources().getString(R.string.reply_label);RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)        .setLabel(replyLabel)        .build();


Provide pre-defined text replies

In addition to voice input, you can also provide up to five texts that can be quickly restored by users. Call setChoices () to pass them to a reply-type array. The following is a defined reply resource array.

  
      
           
    
     Yes
            
    
     No
            
    
     Maybe
        
   
  


Obtain the array and add it to RemoteInput.

public static final 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)        .setLabel(replyLabel)        .setChoices(replyChoices)        .build();


Use voice input as notification Behavior

Use addRemoteInput () to bind the RemoteInput object to an action, so that you can use this notification behavior, as shown in the following code:

// Create an intent for the reply actionIntent replyIntent = new Intent(this, ReplyActivity.class);PendingIntent replyPendingIntent =        PendingIntent.getActivity(this, 0, replyIntent,                PendingIntent.FLAG_UPDATE_CURRENT);// Create the reply action and add the remote inputNotificationCompat.Action action =        new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon,                getString(R.string.label, replyPendingIntent))                .addRemoteInput(remoteInput)                .build();// Build the notification and add the action via WearableExtenderNotification notification =        new NotificationCompat.Builder(mContext)                .setSmallIcon(R.drawable.ic_message)                .setContentTitle(getString(R.string.title))                .setContentText(getString(R.string.content))                .extend(new WearableExtender().addAction(action))                .build();// Issue the notificationNotificationManagerCompat notificationManager =        NotificationManagerCompat.from(mContext);notificationManager.notify(notificationId, notification);


When the notification pops up, the user slides left to see the reply action button.

Receives voice input as a string

To receive voice commands from a user, you can call getResultsFromIntent () to send the commands to the Reply action. This method returns a Bundle containing the text Reply, and then you can query the Bundle containing the Reply. Intent. getExtras () cannot be used here ().

The following code shows how to receive the Intent and return the voice response. It can be used in the above Code.

/** * 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;} 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.