Search code examples
androidkotlinbroadcastreceiver

ACTION_SHUTDOWN not getting called from Broadcast Receiver on android Q (10) and higher


I have the following Broadcast Receiver

 class ShutdownReceiver(): BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        if (Intent.ACTION_SHUTDOWN == intent?.action) {
            HiddenFileUtility.appendLogs("ACTION_SHUTDOWN: true")
            ApplicationGlobalContext.setShutDownState(true)
        }
    }

}

I register the ShutdownReceiver through the AndroidManifext.xml like this:

<receiver android:name=".BroadcastReceivers.ShutdownReceiver">
        <intent-filter android:priority="1">
            <action android:name="android.intent.action.BOOT_COMPLETED" android:priority="999"/>
        </intent-filter>
    </receiver>

And i never receive the ACTION_SHUTDOWN intent.


Solution

  • In the Android official documentation states that As of Build.VERSION_CODES#P this broadcast is only sent to receivers registered through Context.registerReceiver link here

    The solution is to delete the ShutdownReceiver from the AndroidManifest.xml and register it using Context.registerReceiver like this:

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