接收通知中的语音输入(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;}
0 0
原创粉丝点击