1

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.

Răzvan Rujoiu
  • 1,145
  • 12
  • 18

1 Answers1

2

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);
Răzvan Rujoiu
  • 1,145
  • 12
  • 18
  • can you give a proper example for action_shutdown reciever, because in my case it is not working on background , means when we clearing from recent apps list, it is not working , i used foreground services, but no use , still i doesn't work on background – Nns_ninteyFIve Jun 03 '22 at 11:53
  • @Nj_ninteyFIve did you delete the receiver declaration from AndroidManifest.xml? – Răzvan Rujoiu Jun 03 '22 at 17:45
  • @no actually i didn't add a receiver in manifest file , because i have used this receiver inside my foreground service class , here i am sharing the link of my code – Nns_ninteyFIve Jun 04 '22 at 05:22
  • Gabriel Rujoiuhere the code link (https://stackoverflow.com/questions/72461942/broadcast-reciever-action-shutdown-intent-is-not-working-in-android-version-11) – Nns_ninteyFIve Jun 04 '22 at 05:53
  • when you google here it shows the first link of my code "broadcast-reciever-action-shutdown-intent-is-not-working-in-android-version-11" – Nns_ninteyFIve Jun 04 '22 at 05:56
  • @Nj_ninteyFIve From what i have seen in your question, your problem is related with a SMS Receiver. Me personally i also had issues with SMS receivers on Android 11 and above. Try to test your code on lower android versions to isolate the problem. It might be the android version that's causing the problem – Răzvan Rujoiu Jun 04 '22 at 22:40
  • no that's not the issue, when i trying to check the log it doesn't call onReceive method , when i switch off the device, that means shutdown receiver is not working while the app is on background. – Nns_ninteyFIve Jun 05 '22 at 10:42