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;}
- Android Wear 在可穿戴设备中使用语音进行交互
- 谷歌推出可穿戴设备专用系统Android Wear
- 可穿戴设备交互趋势
- Android Wear:创建可穿戴应用 - 概述
- 穿戴设备专用系统Android Wear
- 下载编译 Android wear 源码,尝试制作可穿戴设备功能
- Android Wear之android穿戴式设备应用开发平台
- Android Wear 开发入门——如何创建一个手机与可穿戴设备关联的通知(Notification)
- 全面进军可穿戴市场,Google 宣布 Android Wear 平台
- Android:创建可穿戴应用 - 语音操作
- 可穿戴设备,也许无屏交互才是终极需求!
- 可穿戴设备
- 室内也能用 那些适合在跑步机上使用的可穿戴设备
- 【极客学院出品】Android Wear来袭,全球首发破解可穿戴系统系列课程
- Android Wear:创建可穿戴应用 - 建立模拟器和创建项目
- [Android Wear]安卓穿戴设备Moto 360测评与开发分析
- 站在泡沫之巅的可穿戴设备
- 在android中使用SOAP与webservice进行数据交互
- 百度地图IOS版学习开发流程
- BeanUtils.populate的作用
- Spring3.5 jdbc template API 总结
- Point-In-Time recovery
- phoneGap中android程序自动更新代码(适合Android4.0之后版本)
- Android Wear 在可穿戴设备中使用语音进行交互
- C语言函数参数传递原理
- groovy入门 第04章 列表、映射和范围
- Parcel和Parcelable
- 压力测试
- HNU 12844 Arrangement of Contest
- 前台技术--通过javaScript提交表单
- MySQL下实现闪回的设计思路 (MySQL Flashback Feature)
- 关于Ehcache缓存中timeToLiveSeconds和timeToIdleSeconds