问题描述
限时送ChatGPT账号..似乎(至少)有两种方式可以发送意图 在安卓中:
There seem to be (at least) two ways to send Intents in Android:
PendingIntent.send(...)
Activity.startIntentSenderForResult(
PendingIntent.getIntentSender()
, ...)代码>
除了后者只能从 API 级别 5 开始工作并且结果以不同的方式传回这一事实(通过 PendingIntent.OnFinished
与 Activity.onActivityResult(...)
) 有什么基本的两者的区别?
Other than the fact that the latter only works starting API level 5 and that the results are passed back in a different way (via PendingIntent.OnFinished
vs. Activity.onActivityResult(...)
) is there any fundamental difference between the two?
我发现第一个更方便,因为它可以完全封装在库中,而无需调用 Activity 覆盖 onActivityResult(...)
以转发结果 (像这样:糟糕!).仍然可以使用这种方法吗?
I find the first one a lot more convenient as it can be entirely encapsulated inside a library without requiring the calling activity to override onActivityResult(...)
to forward the result (like this: yuck!). Is it ok to still use that approach?
快速澄清一下,因为我看到有人在另一个问题上抱怨这个: ; 上面的方法不是静态方法.我这样写它们只是为了可读性.
推荐答案
似乎这两种方法非常不同:
Seems like these two approaches are very different:
start...forResult(...)
方法以允许将结果返回给执行 start 的活动的方式启动意图或子活动...forResult(...)
.结果将传递回活动的 onActivityResult(...)
方法.启动意图或子活动的所有其他方式(包括 PendingIntent.send(...)
)都以即发即忘的方式起作用,并且不允许任何结果被退回.OnFinished
处理程序在发送启动后立即调用,无论是否需要一段时间才能完成.因此,传递到此处理程序的数据不一定与您通过 onActivityResult(...)
接收的数据有任何关系.事实上,在我的例子中,OnFinished
处理程序总是在子活动的对话框出现之前立即被调用,resultCode
是 Activity.RESULT_CANCELED
.
The start...forResult(...)
methods start an intent or sub-activity in a way that allows for a result to be returned to the activity that executed the start...forResult(...)
. The result will be passed back to the activity's onActivityResult(...)
method.
All other ways of launching intents or sub-activities (including PendingIntent.send(...)
) act in a fire-and-forget-manner and don't allow for any results to be returned. The OnFinished
handler is called as soon as the launch is sent, whether or not it takes a while to complete. The data passed into this handler, therefore, does not necessarily have anything to do with what you would otherwise receive via onActivityResult(...)
. In fact, in my case, the OnFinished
handler is always called right away, before the dialog of the sub-activity even shows up, with a resultCode
of Activity.RESULT_CANCELED
.
什么乱七八糟的...
这篇关于PendingIntent.send(...) 和 Activity.startIntentSenderForResult(...) 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论