這些是我目前對這些問題的回答。它在許多Google應用程序(地圖,文檔,YouTube,聽)中都是這樣工作的,當您通過麥克風按鈕執行搜索時,所有這些應用程序都將PendingIntent
傳遞到RecognizerIntent
。我不確定這是否是最好的(或最一般的)做法。歡迎任何評論。
從包中提取PendingIntent
的最佳方法是什麼?
Parcelable extraResultsPendingIntentAsParceable =
bundle.getParcelable(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT);
if (extraResultsPendingIntentAsParceable != null) {
if (extraResultsPendingIntentAsParceable instanceof PendingIntent) {
mExtraResultsPendingIntent =
(PendingIntent) extraResultsPendingIntentAsParceable;
} else {
// Report an error
}
}
mExtraResultsPendingIntentBundle =
bundle.getBundle(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE);
如何額外添加到集PendingIntent
現有的臨時演員?
在這裏,我們只是創建一個新的意圖,並把所有必要的額外內容。
if (mExtraResultsPendingIntentBundle == null) {
mExtraResultsPendingIntentBundle = new Bundle();
}
Intent intent = new Intent();
intent.putExtras(mExtraResultsPendingIntentBundle);
// Unsure about the following line...
// Should I use another name for the extra data (instead of SearchManager.QUERY)
intent.putExtra(SearchManager.QUERY, speechRecognitionResult);
如何啓動修改PendingIntent
?
我們發送PendingIntent
給它一個新的意圖(以新的附加)作爲參數。
try {
mExtraResultsPendingIntent.send(this, 1234, intent);
} catch (CanceledException e) {
// Handle exception
}
請檢查下面的鏈接,它是searchDialog.java的來源,你可以深入研究,你可以清楚你的答案。 http://www.devdaily.com/java/jwarehouse/android/core/java/android/app/SearchDialog.java.shtml – Nikhil