Android Wear 在可穿戴设备中使用语音进行交互

来源:互联网 发布:matlab矩阵运算源代码 编辑:程序博客网 时间:2024/04/28 22:19

在通知中接收语音输入

 

如果你手机中创建了一个包括某个行为的通知,如需要回复邮件之类的操作,通常会出现一个activity让用户进行输入,但是再可穿戴设备中,没有任何键盘让用户使用,因此用户进行交互时使用的输入方式可以使用RemoteInput。

当用户使用语音回复或者可支持的其他输入方式是,系统会将文本的答复绑定在你指定的通知行为的Intent中,然后将该Intent传入手机的app。

 

定义语音输入

为了创建一个可以接受语音输入的行为,需要实例化RemoteInput.Builder并添加到通知的action中。它的构造方法接收一个字符串,这个字符串是系统使用语音输入的key,这样之后,就可以使用语音输入与app进行关联了。

例如,下面的代码就是创建一个RemoteInput对象,并提供语音输入功能的方法。

// 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();


提供预定义的文本回复

除了支持语音输入,你也能提供最多5个让用户快速恢复的文本,调用setChoices()传递给一个字符型数组,如下是定义的一个回复的资源数组

<?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 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();


将语音输入作为通知行为

使用addRemoteInput()将RemoteInput对象绑定到一个action中,这样你就能使用这个通知行为了,如下代码所示:

// 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);


当通知弹出的时候,用户向左滑动屏幕,就可以看到reply的行为按钮。

 

接收语音输入作为字符串

接收用户说出的语音指令,可以调用getResultsFromIntent(),传递给Reply行为,这个方法返回一个Bundle,包含了文本的回复,然后可以查询携带回复的Bundle。这里不能用Intent.getExtras()

下面的代码展示了接收Intent与返回语音应答的方法,在上面的代码中可以使用。

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


3 2
原创粉丝点击