-2

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 ...

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • If you mean that you're forcibly stopping your app, then that's expected behavior, since Android 3.1: https://stackoverflow.com/a/19856367. – Mike M. Mar 24 '22 at 16:56
  • 1
    @MikeM. Thanks for that comment. I'm pretty sure, i visited that question, but obviously didn't scroll far enough ... – derpirscher Mar 24 '22 at 21:11

1 Answers1

2

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • 1
    I implemented a job scheduler. while persisting across reboot seems to work, it of course also doesn't resolve the initial problem, ie my notifications getting lost when the app is forcely closed. but that seems to be by design... Furthermore with job schedulers I haven't found a way yet to show notifications which where scheduled while the device was powered off – derpirscher Mar 25 '22 at 00:01