接收通知中的语音输入(Receiving Voice Input in a Notification)
来源:互联网 发布:64位 mysql 编辑:程序博客网 时间:2024/06/05 18:05
接收通知的语音输入
如果手机上的通知包含输入文本的操作,比如回复email那么通常是启动一个页面输入文本.但是在穿戴设备wear上,没有键盘输入所以只能使用RemoteInput这个类完成让使用者指定回复或者回复预定义的文本信息.
当用户使用语音或者选择一个预定义消息回复时,系统会将回复内容信息绑定到为通知创建的Intent
上,并且发送到手机app上.
注意:android模拟器不支持语音输入,当使用wear的模拟器时,需要启用AVD设置中的显示硬件键盘,代替语音回复.
定义语音输入
首先创建RemoteInput.Builder然后为通知添加语音输入行为.这个类的构造方法接收一个字符串,这个字符串做为输入语音内容的key,在之后获取语音输入内容时需要使用这个key.
举个例子,展示如何创建一个RemoteInput对象.
// Key for the string that's delivered in the action's intentprivate 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() 这个方法同时需要传递一个字符串数组做为参数.
举个例子,你可以在数组中定义一些回复内容.
<?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();
添加语音输入做为通知的行为
使用addRemoteInput()这个方法绑定到RemoteInput对象上去设置语音输入.可以将这个行为应用到通知上.举个栗子:
// 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);
当展示这个通知的时候,用户可以向左滑动看到回复的行为按钮.
获取语音输入做为一个字符串
为了在你定义的页面中收到用户发送传递的消息里的intent
,通过调用getResultsFromIntent()这个方法获得回复消息里的intent
.这个方法返回一个包含回复文本内容的Bundle
,你可以查询获得这个Bundle
中的回复.
注意:不要使用Intent.getExtras()去获取语音输入的东西,因为语音输入内容被做为ClipData来存储.而方法getResultsFromIntent()提供了一个便利的方式去获取收到的字符串避免了你自己处理ClipData数据的过程.
下面的代码展示获取intent
和语音内容的方式,这里引用到了上面代码片段中的EXTRA_VOICE_REPLY
做为key.
/** * 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;}
- 接收通知中的语音输入(Receiving Voice Input in a Notification)
- Android Wear Preview- 从通知上接收语音输入(Receiving Voice Input from a Notification)
- Voice input 语音输入
- 国人制作的 Chrome 浏览器语音输入扩展 Voice Input
- Android中的通知Notification
- Android中的Notification(通知)
- Android中的通知Notification
- Android中的通知Notification
- Android中的通知 --- Notification
- Android中的通知 Notification
- Android中的通知Notification
- android中的通知Notification
- iOS10中的通知Notification
- Android中的通知Notification
- android中的通知Notification
- Android中的通知notification
- Android中的通知Notification
- PO Receiving: 从接收到入库过程中的财务处理
- 超分辨率重建——先验信息,先验概率,后验概率,最大后验概率
- 找链表的倒数第k个节点
- 小米宋强:生态化大数据平台的深度应用实践
- javascript-base
- 如何以最快的速度计算出一个二进制数中1的个数
- 接收通知中的语音输入(Receiving Voice Input in a Notification)
- 如何蒸螃蟹?教你蒸螃蟹3个小窍门
- 面向对象基础(一)
- “呼死你”重出江湖 如何避免骚扰电话?
- VHDL_3641BS四个7段数码管显示实验
- 在Angular中使用promise
- Android_app完美退出程序
- CRectTracker(橡皮筋)类的使用
- 理解Java传参