Receive voice input in notifications
If you create a notification in your phone that includes a behavior, such as a reply to a message, there is usually an activity for the user to input, but on the wearable device, there is no keyboard for the user to use, so the input used when the user interacts can use Remoteinput.
When a user uses voice replies or other input that can be supported, the system binds the reply of the text in the intent of the notification behavior you specify, and then passes the intent to the app of the phone.
Define speech input
In order to create a behavior that accepts speech input, you need to instantiate the remoteinput.builder and add it to the action of the notification. Its construction method receives a string that is a key used by the system to use speech input, so that it can then be associated with the app using voice input.
For example, the following code is a way to create a Remoteinput object and provide speech input functionality.
Key for the string that ' s Deliveredin the action ' s intentprivate staticfinalstring extra_voice_reply= "Extra_voice_repl Y "; 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 support for voice input, you can also provide up to 5 text to allow users to quickly recover, call setchoices () passed to a character array, as follows is a definition of a reply to the resource array
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string-array name= "Reply_choices" > <item>Yes</item> <item>No</item> <item>Maybe</item> </ String-array></resources>
The array is then fetched and added to the 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 speech input as a 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.FLA g_update_current)//Create The Reply action and add the remote inputnotificationcompat.action action = new notific AtionCompat.Action.Builder (R.drawable.ic_reply_icon, getString (R.string.label, replypendingintent)) . Addremoteinput (Remoteinput). build ();//build the notification and add the action via Wearableexte Ndernotification notification = new Notificationcompat.builder (mcontext). Setsmallicon (R.drawable.ic _message). Setcontenttitle (GetString (R.string.title)). Setcontenttext (GetString (R.string.con Tent). Extend (New Wearableextender (). Addaction (Action)). build ();//Issue the Notificationn Otificationmanagercompat Notificationmanager = Notificationmanagercompat.from (Mcontext); notificationmanager.notify (notificationid, notification);
When the notification pops up, the user slides the screen to the left to see the reply's Behavior button.
Receive speech input as a string
Receive the voice instruction that the user speaks, can call getresultsfromintent (), pass to the reply behavior, this method returns a bundle, contains the reply of the text, then can query the bundle that carries reply. Intent.getextras ()cannot be used here.
The following code shows the method of receiving intent and returning a voice response, which can be used in the code above.
/** * 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);} }