gpt4 book ai didi

android - 动态注册的 BroadcastReceivers 是否在组件/应用程序死亡后仍然存在?

转载 作者:太空狗 更新时间:2023-10-29 13:26:35 26 4
gpt4 key购买 nike

在 Activity 中注销动态注册的 BroadcastReceiver 失败有什么影响? it runonReceive() 是否会在 Activity 被销毁后被调用(如果触发与其 intent 过滤器匹配的广播则为 ofc)?即使在进程被杀死之后?


相关(docs)

If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.

这是否意味着 BR 仅在 onPause 运行之前处于 Activity 状态(如果在 Activity 中注册)正如我在 Gingerbread 中的副手实验所建议的那样(它们确实是副手) - 或者如果我取消注册那么我将不会收到 Intent (如前所述here)?

编辑:实际上我 posted这是文档的一个问题

最佳答案

What implications has a failure to unregister a dynamically registered BroadcastReceiver in an activity ?

我怀疑可能会有一些可能的结果,但这取决于 BroadcastReceiver 注册的目的以及它是独立类还是 Activity 的内部类

Will it run even after the activity is destroyed ?

不清楚您所说的“运行”是什么意思。从技术上讲,注册的 BroadcastReceiver 并不是纯粹因为注册而“运行”的。有效的注册只是将一个“钩子(Hook)”放入 Android 消息传递系统中,并将 onReceive(...) 方法作为回调。如果有一个广播与 BroadcastReceiver 的 Intent 过滤器相匹配,那么它的“运行”生命周期是从调用 onReceive(...) 的开始到结束。

Even after the process is killed ?

没有。由于 Android 应用程序的所有组件都在单个进程中运行(除了一些异常(exception),例如进程外的 Services),一旦进程被终止,BroadcastReceiver 将被它杀死。

So does that mean that BRs are only active till onPause runs (if registered in an activity)...

正如我在上面提到的,BroadcastReceiver 仅在其 onReceive(...) 方法生命周期内处于“Activity ”状态 - 在其他时间,它不会被实例化并且只是处于“已注册”状态。

... or that if I unregister then I won't receive intents (as said here) ?

同样,正如我上面提到的,注册只是告诉操作系统接收者希望接收与 Intent 过滤器匹配的通知(广播)。如果它未注册,则系统会删除该特定 BroadcastReceiver 类的“ Hook ”(回调),因此它不会被实例化并传递相关的 Intent

关于android - 动态注册的 BroadcastReceivers 是否在组件/应用程序死亡后仍然存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20712220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com