RecognizerIntent:如何将捆绑添加到待处理的意图
我正在实现一个响应 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 aPendingIntent
, the results will be added to its bundle and thePendingIntent
will be sent to its target.If you use
EXTRA_RESULTS_PENDINGINTENT
to supply a forwarding intent, you can also useEXTRA_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
出于安全原因,您不能直接触摸 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.
我也许可以为您的第二个问题提供一些帮助,因为我在自己的应用程序中做了类似的事情。
向意图添加额外内容应该像在意图上调用 putExtra() 一样简单。我这样做是为了通知。
意图 notificationIntent = new Intent(_context, MyActivity.class);
notificationIntent.putExtra("SOME_ID", "VALUE");
这是启动我的应用程序的通知的一部分。后来当我的活动恢复时,我读了额外的内容。
希望这对一些人有帮助。
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.
Hope this helps some.
这些是我目前对这些问题的回答。它在许多 Google 应用程序(地图、文档、YouTube、Listen)中的工作原理如下,当您通过麦克风按钮执行搜索时,这些应用程序都会将
PendingIntent
传递给RecognizerIntent
。我不确定这是否是最好的(或最通用的)方法。欢迎任何评论。从捆绑包中提取
PendingIntent
的最佳方式是什么?如何将额外内容添加到
PendingIntent
的现有额外内容集中?这里我们只是创建一个新的意图并将所有必需的附加内容放入其中。
如何启动修改后的
PendingIntent
?我们发送
PendingIntent
,为其提供新意图(带有新的附加内容)作为参数。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 theRecognizerIntent
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?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.
How to launch the modified
PendingIntent
?We send off the
PendingIntent
giving it the new intent (with the new extras) as an argument.