11

我正在实施一个响应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)

如何在 a 的现有附加项集中添加附加项PendingIntent

如何启动修改PendingIntent

4

3 回答 3

4

出于安全原因,您不能直接触摸 PendingIntent 的内容。但是,当您发送 PendingIntent 时,您有机会根据原始创建者的允许来补充或修改其内容。

这是您要用来发送 PendingIntent 的方法:

http://developer.android.com/reference/android/app/PendingIntent.html#send(android.content.Context, int, android.content.Intent, android.app.PendingIntent.OnFinished, android.os.Handler)

您在此处提供的 Intent 是用于修改从 PendingIntent 发送的最终 Intent 的数据。

可以修改的规则在这里:

http://developer.android.com/reference/android/content/Intent.html#fillIn(android.content.Intent, int)

请注意,默认情况下,当创建 PendingIntent 时,发送方唯一可以修改的部分是附加部分。创建者可以传入标志以允许修改其他部分,尽管这通常是不希望的。

于 2011-07-01T00:57:11.033 回答
1

这些是我目前对这些问题的回答。它在许多 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
}
于 2011-07-01T09:24:50.007 回答
1

我可能可以为您的第二个问题提供一些帮助,因为我在自己的应用程序中做了类似的事情。

向意图添加额外内容应该像在意图上调用 putExtra() 一样简单。我这样做是为了通知。

Intent notificationIntent = new Intent(_context, MyActivity.class); notificationIntent.putExtra("SOME_ID", "VALUE");

这是启动我的应用程序的通知的一部分。我后来在我的活动恢复时阅读了额外的内容。

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras !=null)
{
   String value = extras.getString("SOME_ID");
   if( value != null && value.equals( "VALUE" ) )
   {
      //Value was found, do something
   }
}

希望这会有所帮助。

于 2011-06-27T19:28:32.047 回答