【问题标题】:ACTION_SHUTDOWN not getting called from Broadcast Receiver on android Q (10) and higherACTION_SHUTDOWN 未从 android Q (10) 及更高版本上的广播接收器调用
【发布时间】:2021-04-02 08:51:37
【问题描述】:

我有以下广播接收器

 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 kotlin broadcastreceiver


    【解决方案1】:

    Android官方文档中声明As of Build.VERSION_CODES#P this broadcast is only sent to receivers registered through Context.registerReceiverlink here

    解决方案是从AndroidManifest.xml 中删除ShutdownReceiver 并使用Context.registerReceiver 注册它,如下所示:

    val shutdownReceiver = ShutdownReceiver();
    val bootIntentFilter = IntentFilter(Intent.ACTION_SHUTDOWN);
    context.registerReceiver(shutdownReceiver, bootIntentFilter);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多