RecognizerIntent:如何将捆绑包添加到待处理的意图

Kaa*_*rel 10 android bundle recognizer-intent android-pendingintent

我正在实现一个响应RecognizerIntent的活动.除此之外,此活动必须处理两个指定待处理意图附加附加内容及其附加内容:

  • EXTRA_RESULTS_PENDINGINTENT
  • EXTRA_RESULTS_PENDINGINTENT_BUNDLE

解释文档:

  • 如果您使用EXTRA_RESULTS_PENDINGINTENT提供a PendingIntent,则结果将添加到其包中,PendingIntent并将结果发送到其目标.

  • 如果您使用EXTRA_RESULTS_PENDINGINTENT提供转发意图,您还可以使用EXTRA_RESULTS_PENDINGINTENT_BUNDLE为最终意图提供额外的额外内容.搜索结果将添加到此捆绑包中,组合捆绑包将发送到目标.

我一直在寻找可以证明以下内容的示例代码.

PendingIntent从捆绑中提取a的最佳方法是什么?

我应该这样做:

(PendingIntent)
        extras.getParcelable(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT)
Run Code Online (Sandbox Code Playgroud)

如何在一组现有的附加内容中添加额外内容PendingIntent

如何启动修改PendingIntent

Kaa*_*rel 1

这些是我目前对这些问题的回答。它在许多 Google 应用程序(地图、文档、YouTube、Listen)中都是这样工作的,当您通过麦克风按钮执行搜索时,这些应用程序都会将 传递PendingIntentRecognizerIntent。我不确定这是否是最好的(或最通用的)方法。欢迎任何评论。

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);
Run Code Online (Sandbox Code Playgroud)

如何将额外内容添加到 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);
Run Code Online (Sandbox Code Playgroud)

如何启动修改后的PendingIntent

PendingIntent我们将赋予它新的意图(带有新的额外内容)作为参数发送出去。

try {           
    mExtraResultsPendingIntent.send(this, 1234, intent);
} catch (CanceledException e) {
    // Handle exception
}
Run Code Online (Sandbox Code Playgroud)