W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
編寫:wangyachen - 原文:http://developer.android.com/training/wearables/notifications/voice-input.html
如果手持式設(shè)備上的Notification包含了一個(gè)輸入文本的action,比如回復(fù)郵件,那么這個(gè)action正常情況下應(yīng)該會(huì)調(diào)起一個(gè)activity讓用戶進(jìn)行輸入。但是,當(dāng)這個(gè)action出現(xiàn)在可穿戴式設(shè)備上時(shí),是沒(méi)有鍵盤可以讓用戶進(jìn)行輸入的,所以開發(fā)者應(yīng)該讓用戶指定一個(gè)反饋或者通過(guò)RemoteInput預(yù)先設(shè)定好文本信息。
當(dāng)用戶通過(guò)語(yǔ)音或者選擇可見的消息進(jìn)行回復(fù)時(shí),系統(tǒng)會(huì)將文本的反饋信息與開發(fā)者指定的Notification中的action中的Intent進(jìn)行綁定,并且將該intent發(fā)送給手持設(shè)備中的app。
Note:Android模擬器并不支持語(yǔ)音輸入。如果使用可穿戴式設(shè)備的模擬器的話,可以打開AVD設(shè)置中的Hardware keyboard present,實(shí)現(xiàn)用打字代替語(yǔ)音。
為了創(chuàng)建一個(gè)支持語(yǔ)音輸入的action,需要?jiǎng)?chuàng)建一個(gè)RemoteInput.Builder的實(shí)例,將其加到Notification的action中。這個(gè)類的構(gòu)造函數(shù)接受一個(gè)String類型的參數(shù),系統(tǒng)用這個(gè)參數(shù)作為語(yǔ)音輸入的key,后面我們會(huì)用這個(gè)key來(lái)取得在手持設(shè)備中輸入的文本。
舉個(gè)例子,下面展示了如何創(chuàng)建一個(gè)RemoteInput對(duì)象,其中,該提供了一個(gè)用于提示語(yǔ)音輸入的自定義label。
// Key for the string that's delivered in the action's 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();
除了要打開語(yǔ)音輸入支持之外,開發(fā)者還可以提供多達(dá)5條的文本反饋,這樣用戶可以直接選擇實(shí)現(xiàn)快速回復(fù)。該功能可通過(guò)調(diào)用setChoices())并傳遞一個(gè)String數(shù)組實(shí)現(xiàn)。
舉個(gè)例子,可以用resource數(shù)組的方式定義這些反饋:
res/values/strings.xml
<?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>
然后,填充 String 數(shù)組,并將其添加到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();
為了實(shí)現(xiàn)設(shè)置語(yǔ)音輸入,可以把RemoteInput對(duì)象通過(guò)addRemoteInput())設(shè)置到一個(gè)action中。然后我們可以將這個(gè)action應(yīng)用到Notification中,例如:
// Create an intent for the reply action
Intent 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 input
NotificationCompat.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 WearableExtender
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();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext);
notificationManager.notify(notificationId, notification);
當(dāng)程序發(fā)出這個(gè)Notification的時(shí)候,用戶在可穿戴設(shè)備上左滑便可以看到reply的按鈕。
通過(guò)調(diào)用getResultsFromIntent())方法,將返回值放在"Reply"的action指定的intent中,開發(fā)者便可以在回復(fù)的action的intent中指定的activity里,接收到用戶轉(zhuǎn)錄后的消息。該方法返回的是包含了文本反饋的Bundle。我們可以通過(guò)查詢Bundle中的內(nèi)容來(lái)獲得這條反饋。
Note:請(qǐng)不要使用Intent.getExtras())來(lái)獲取語(yǔ)音輸入的結(jié)果,因?yàn)檎Z(yǔ)音輸入的內(nèi)容是存儲(chǔ)在ClipData中的。getResultsFromIntent())提供了一條很方便的途徑來(lái)接收字符數(shù)組類型的語(yǔ)音信息,并且不需要經(jīng)過(guò)ClipData自身的調(diào)用。
下面的代碼展示了一個(gè)接收intent,并且返回語(yǔ)音反饋信息的方法,該方法是依據(jù)之前例子中的EXTRA_VOICE_REPLY
作為key進(jìn)行檢索:
/**
* 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;
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: