【问题标题】:RecognizerIntent: how to add a bundle to a pending intentRecognizerIntent:如何将捆绑包添加到待处理的意图
【发布时间】:2011-06-24 10:08:21
【问题描述】:

我正在实施一个响应RecognizerIntent 的活动。除其他外,此活动必须处理两个传入的附加内容,这些附加内容指定了待处理的意图和附加包:

  • EXTRA_RESULTS_PENDINGINTENT
  • EXTRA_RESULTS_PENDINGINTENT_BUNDLE

解释文档:

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

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

我一直在徒劳地寻找可以演示以下内容的示例代码。

从包中提取PendingIntent 的最佳方法是什么?

我应该这样做:

(PendingIntent)
        extras.getParcelable(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT)

如何向PendingIntent 的现有附加信息集添加附加信息?

修改后的PendingIntent如何启动?

【问题讨论】:

标签: android bundle android-pendingintent recognizer-intent


【解决方案1】:

出于安全原因,您不能直接触摸 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 时,发送方唯一可以修改的部分是 extras。创建者可以传入标志以允许修改其他部分,尽管这通常是不希望的。

【讨论】:

  • 谢谢,这就是我想要的。
  • 不客气。我应该澄清一下——填写 Intent 的默认规则是只能修改附加项,但如果其他字段尚未包含数据,则可以设置它们。例如,默认情况下,如果已设置,您将无法更改操作。 PendingIntent 的创建者可以使用标志来表示允许发件人修改该字段......但这当然会给他们带来重大的安全问题,因为这意味着无论他们给 PendingIntent 的人是谁都可以修改它来做一些非常不同的事情.
  • 如果我使用 PendingIntent.getBroadcast 来获取 PI,我需要哪些特殊步骤来强制它保留 PI 最初创建时使用的现有 Intent extra?我正在尝试上述方法,但额外内容总是为空。
【解决方案2】:

我可能会在您的第二个问题上为您提供一点帮助,因为我在自己的应用中做过类似的事情。

向意图添加额外内容应该像在意图上调用 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
   }
}

希望这会有所帮助。

【讨论】:

  • 谢谢,但是 PendingIntent 和 Intent 不太一样。
【解决方案3】:

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

从包中提取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
}

【讨论】:

  • @IgorG。我不确定我是否理解......上述代码的所有输入都来自额外内容。
猜你喜欢
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多