derpirscher
derpirscher

Reputation: 17400

BOOT_COMPLETED Broadcast not received when app was killed

I'm stuck at the development of our app. I want to receive the BOOT_COMPLETED broadcast so I can reschedule some Alarms. So I start my app, schedule the alarms and then reboot my device (Android 10). Everything works as expected, ie after reboot, my BroadcastReceiver is started and my alarms are rescheduled.

But if I stop my app before reboot (ie not just put it in background, really kill it), it doesn't receive the BOOT_COMPLETED broadcast anymore ...

I have the respective permissions in my manifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

I have the receiver in my manifest enabled with the respective actions

<receiver
  android:name=".receivers.CustomBroadcastReceiver"
  android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
  android:enabled="true"
  android:exported="true"
  android:directBootAware="false">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
</receiver>

And this is my CustomBroadcastReceiver class

class CustomBroadcastReceiver : BroadcastReceiver() {

  override fun onReceive(context: Context?, intent: Intent?) {
    Log.d(javaClass.simpleName, "onReceive ${intent.action}")
    ... //reschedule alarms read from SQLite
  }

}

I can see the respective Log message after reboot, when the app was running before reboot. But I don't see the log, when the app was killed before the reboot ... Anything I'm missing here?

Yes, I'm aware, there are many other questions regarding BOOT_COMPLETED but they all more or less say, do it like shown above, then it will work. And, well, I know it in principle does work, because I see the receiver being called. But just not, when the app was once killed. If I start the app manually, put it in background and reboot the device, it works as expected ...

Upvotes: -4

Views: 1990

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93668

Use JobScheduler instead of alarms for this. JobScheduler jobs can be set as persistent across boot, so you don't have to do the bootup wiring yourself.

Upvotes: 2

Related Questions