ACTION_SHUTDOWN 在 android Q (10) 及更高版本上未收到来自 Broadcast Receiver 的调用
ACTION_SHUTDOWN not getting called from Broadcast Receiver on android Q (10) and higher
我有以下广播接收器
class ShutdownReceiver(): BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (Intent.ACTION_SHUTDOWN == intent?.action) {
HiddenFileUtility.appendLogs("ACTION_SHUTDOWN: true")
ApplicationGlobalContext.setShutDownState(true)
}
}
}
我通过 AndroidManifext.xml
注册 ShutdownReceiver
是这样的:
<receiver android:name=".BroadcastReceivers.ShutdownReceiver">
<intent-filter android:priority="1">
<action android:name="android.intent.action.BOOT_COMPLETED" android:priority="999"/>
</intent-filter>
</receiver>
而且我从未收到 ACTION_SHUTDOWN
意图。
在 Android
官方文档中指出 As of Build.VERSION_CODES#P this broadcast is only sent to receivers registered through Context.registerReceiver
link here
解决方法是从 AndroidManifest.xml
中删除 ShutdownReceiver
并使用 Context.registerReceiver
注册它,如下所示:
val shutdownReceiver = ShutdownReceiver();
val bootIntentFilter = IntentFilter(Intent.ACTION_SHUTDOWN);
context.registerReceiver(shutdownReceiver, bootIntentFilter);
我有以下广播接收器
class ShutdownReceiver(): BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (Intent.ACTION_SHUTDOWN == intent?.action) {
HiddenFileUtility.appendLogs("ACTION_SHUTDOWN: true")
ApplicationGlobalContext.setShutDownState(true)
}
}
}
我通过 AndroidManifext.xml
注册 ShutdownReceiver
是这样的:
<receiver android:name=".BroadcastReceivers.ShutdownReceiver">
<intent-filter android:priority="1">
<action android:name="android.intent.action.BOOT_COMPLETED" android:priority="999"/>
</intent-filter>
</receiver>
而且我从未收到 ACTION_SHUTDOWN
意图。
在 Android
官方文档中指出 As of Build.VERSION_CODES#P this broadcast is only sent to receivers registered through Context.registerReceiver
link here
解决方法是从 AndroidManifest.xml
中删除 ShutdownReceiver
并使用 Context.registerReceiver
注册它,如下所示:
val shutdownReceiver = ShutdownReceiver();
val bootIntentFilter = IntentFilter(Intent.ACTION_SHUTDOWN);
context.registerReceiver(shutdownReceiver, bootIntentFilter);