关于Android O更改的BroadcastReceiver的生命周期 [英] Lifetime of BroadcastReceiver with regard to Android O changes
问题描述
如果我通过mainfest文件为系统广播声明了BroadcastReceiver
(例如ACTION_POWER_DISCONNECTED
),则每次发送特定广播时系统都会调用它,因此BroadcastReceiver的生存期不受限制./p>
但是,有些广播无法通过清单文件进行注册.对于这些广播,我们必须使用相应的IntentFilter
调用context.registerReceiver
.假设我为BOOT_COMPLETED
创建了BroadcastReceiver并从中调用context.registerReceiver
而从不调用unregisterReceiver
,那么该接收器是否也可以永久存在(直到电话重启)?
面向Android O的应用程序无法再在其清单中为隐式广播注册广播接收器.隐式广播是一种不专门针对该应用的广播.
如果从上面我的猜想是正确的,那么这将是系统更改的简单解决方法(当然,您不应该这样做,但有可能).那么,在BOOT_COMPLETED
广播之后注册的BroadcastReceiver
是否具有与通过清单自动注册的BroadcastReceiver相同的生存期(一直保持到下次重新启动)?
假设我为
BOOT_COMPLETED
创建了BroadcastReceiver
并从中调用context.registerReceiver
而从不调用unregisterReceiver
,那么此接收器是否也可以永久存在(直到电话重启)?
首先,BOOT_COMPLETED
是其中一项操作,仍然会像以前一样运行,这意味着"O"中引入的限制与该操作无关.
一旦您的应用程序进程被系统终止或系统清除了您的应用程序内存(由于设备内存不足),您的广播注册就会丢失.否则,我将看不到此限制如何带来更好的电池使用体验.
BOOT_COMPLETED
广播之后注册的BroadcastReceiver
是否与通过清单自动注册的BroadcastReceiver
具有相同的生存期(一直保持到下次重新启动)?
如果不满足上述情况,即您的应用程序处于活动状态,并且由于内存不足而无法从内存中清除应用程序-那么可以.进入缓存状态(没有活动的Android的状态)后,组件)注册将再次丢失.
Nasir Khan的这段短视频会很有帮助.
If I declare a BroadcastReceiver
via the mainfest file for a system broadcast (let's say for example ACTION_POWER_DISCONNECTED
) the the system will call it every time the specific broadcast is send so the lifetime of the BroadcastReceiver is unrestricted.
But there are also broadcasts which can not be registered via the manifest file. For these broadcasts we have to call context.registerReceiver
with a corresponding IntentFilter
. Let's say I create a BroadcastReceiver for BOOT_COMPLETED
and call context.registerReceiver
from it and never call unregisterReceiver
does this receiver also lives forever (until the phone is rebooted)?
Apps that target Android O can no longer register broadcast receivers for implicit broadcasts in their manifest. An implicit broadcast is a broadcast that does not target that app specifically.
If my conjecture from above is right this would be an easy workaround for the system change (of course you shouldn't do it this way but it would be possible). So does a BroadcastReceiver
which is registered after a BOOT_COMPLETED
broadcast have the same lifetime (stays until the next reboot) as a BroadcastReceiver which is automatically registered via the manifest?
Let's say I create a
BroadcastReceiver
forBOOT_COMPLETED
and callcontext.registerReceiver
from it and never callunregisterReceiver
does this receiver also lives forever (until the phone is rebooted)?
First, BOOT_COMPLETED
is one of those actions, that still will behave like they were before, meaning restriction introduced in "O" do not concern to that action.
As soon as the process of your app is killed by the system or as soon as system clears your app's memory (as a result of low-memory of device), your broadcast registration will be lost. Otherwise I cannot see how this limitation will result in a better battery experience.
So does a
BroadcastReceiver
which is registered after aBOOT_COMPLETED
broadcast have the same lifetime (stays until the next reboot) as aBroadcastReceiver
which is automatically registered via the manifest?
If above mentioned cases are not met, i.e. the process of your app stays alive and app is not cleared from memory because of memory shortage - then yes. Once entered into cached state (the state with no active Android component) the registration will be lost again.
This short video by Nasir Khan will be helpful.
这篇关于关于Android O更改的BroadcastReceiver的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!