这些是我目前对这些问题的回答。它在许多 Google 应用程序(地图、文档、YouTube、Listen)中都是这样工作的,当您通过麦克风按钮执行搜索时,它们都会传递PendingIntent
给您。RecognizerIntent
我不确定这是否是最好的(或最通用的)方法。欢迎任何意见。
PendingIntent
从捆绑中提取 a 的最佳方法是什么?
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);
如何在 a 的现有附加项集中添加附加项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
}