在通知中接受语音输入
以下内容翻译自android wear的官方教程,本人水平有限,如有错误欢迎指出
home
以下正文
当你的手机上收到一个需要输入文本的消息,比如回复一个email的时候,你可以启动一个activity来输入这个文本。但是,如果这个消息出现在手表上,那可就没有键盘给你打字了,所以你要让用户可以口述回信或提供一个快速回复的回信列表,这两个功能可以通过RemoteInput对象实现。
当用户口述一段声音或选择一个快速回复消息的时候,系统就会将文本放到你在notification中指定的intent,然后把这个intent发送到你的手机。
注意:android 模拟器不支持声音输入,当在用模拟器时,请在AVD设置中打开Hardware keyboard present,然后用键盘输入文本
![](https://img.haomeiwen.com/i3293174/b9ab93f1db99eff9.png)
![](https://img.haomeiwen.com/i3293174/0ac962793a35f3d8.png)
定义声音输入
为了创建一个支持声音输入的action,实例化RemoteInput.Builder来加入你的notification action,这个类的构造函数接受一个String,这个字符串是声音输入的key,你将会在手机上获取从手表上发来的文本的时候用到这个key。
比如,这里创建了一个RemoteInput对象并添加了一个在声音输入时的提示信息
// 字符串key将被发送到action的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)
.setLabel(replyLabel)
.build();
添加快速回复文本信息
除了可以声音输入外,你也可以提供五个快速回复文本。将字符串数组传给setChoices()并调用。
![](https://img.haomeiwen.com/i3293174/7352f1941f9bbc7d.png)
比如,你可以在资源里定义快速回复信息:
<?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>
然后把这个数组塞给RemoteInput:
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)
.setLabel(replyLabel)
.setChoices(replyChoices)
.build();
将声音输入添加到Notification Action当中
把你的RemoteInput送给notification。
// 为回复动作新建一个intent
Intent replyIntent = new Intent(this, ReplyActivity.class);
PendingIntent replyPendingIntent =
PendingIntent.getActivity(this, 0, replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// 创建回复动作,然后将remoteInput放进去
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon,
getString(R.string.label), replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
// 新建notification并通过WearableExtender将action添加进去
Notification 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();
// 发送通知
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext);
notificationManager.notify(notificationId, notification);
你发送完这条信息后,用户可以左滑然后看到一个"Reply"动作按钮
将声音输入当作文本
在你手机上被启动的activity中用getResultsFromIntent()方法获得一个Bundle。然后用这个Bundle来取得回复信息。
注意不要用Intent.getExtras()来获取声音结果,因为声音输入是ClipData。getResultsFromIntent()方法会帮你处理ClipData。
下面的代码展示了怎么样通过intent获取声音输入,注意那个key在这里用到了
// 获取intent的方法是调用 Activity.getIntent() ,因为是这个intent启动了这个Acitvity
private CharSequence getMessageText(Intent intent) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
return remoteInput.getCharSequence(EXTRA_VOICE_REPLY);
}
return null;
}