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

发布于 2024-11-17 03:31:57 字数 1016 浏览 3 评论 0原文

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

I am implementing an activity that responds to the RecognizerIntent. Among others this activity must handle two incoming extras that specify a pending intent and its extras-bundle:

  • EXTRA_RESULTS_PENDINGINTENT
  • EXTRA_RESULTS_PENDINGINTENT_BUNDLE

Paraphrasing the documentation:

  • If you use EXTRA_RESULTS_PENDINGINTENT to supply a PendingIntent, the results will be added to its bundle and the PendingIntent will be sent to its target.

  • If you use EXTRA_RESULTS_PENDINGINTENT to supply a forwarding intent, you can also use EXTRA_RESULTS_PENDINGINTENT_BUNDLE to supply additional extras for the final intent. The search results will be added to this bundle, and the combined bundle will be sent to the target.

I have been looking in vain for sample code that would demonstrate the following.

What is the best way of extracting a PendingIntent from a bundle?

Should I do:

(PendingIntent)
        extras.getParcelable(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT)

How to add extras to the set of existing extras of a PendingIntent?

How to launch the modified PendingIntent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满身野味 2024-11-24 03:31:57

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

You can not directly touch the contents of a PendingIntent, for security reasons. However, when you send the PendingIntent, you have the opportunity to supplement or modify its contents depending on what the original creator allows.

This is the method you want to use to send the 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)

The Intent you supply here is the data used to modify the final Intent sent from the PendingIntent.

The rules for what can be modified are here:

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

Note that by default when a PendingIntent is created, the only parts that can be modified by the sender are the extras. The creator can pass in flags to allow other parts to be modified, although this is generally not desired.

逆夏时光 2024-11-24 03:31:57

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

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

意图 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
   }
}

希望这对一些人有帮助。

I might be able to give you a little help with your second question as I have done something similar in my own app.

Adding extras to an intent should be as easy as calling putExtra() on the intent. I have done this for a notification.

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

This is part of a notification that launches my app. I later read the extra when my activity resumes.

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
   }
}

Hope this helps some.

醉生梦死 2024-11-24 03:31:57

这些是我目前对这些问题的回答。它在许多 Google 应用程序(地图、文档、YouTube、Listen)中的工作原理如下,当您通过麦克风按钮执行搜索时,这些应用程序都会将 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
}

These are my current answers to these questions. It works like this in a number of Google apps (Maps, Docs, YouTube, Listen) which all pass the PendingIntent to the RecognizerIntent when you perform the search via the microphone button. I am unsure though if this is the best (or most general) way of doing it. Any comments are welcome.

What is the best way of extracting a PendingIntent from a bundle?

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);

How to add extras to the set of existing extras of a PendingIntent?

Here we just create a new intent and put all the required extras into it.

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);

How to launch the modified PendingIntent?

We send off the PendingIntent giving it the new intent (with the new extras) as an argument.

try {           
    mExtraResultsPendingIntent.send(this, 1234, intent);
} catch (CanceledException e) {
    // Handle exception
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文