0

I have a table with notification contents. One entry is always marked as isNext, until none are left.
The days between the notifications vary and are also saved in said table, so I am using a "chain" of alarms, instead of one repeating alarm.

db screenshot

Now I read that all alarms of a manager will be reset after device reboot (does Alarm Manager persist even after reboot?).

Now it seems there are be two ways to solve this:

  • Check every time if the alarm is set already and if not create it again, as asked here too: How to check if AlarmManager already has an alarm set?
  • Or, as shown in the question above that, register a BroadcastReceiver with <action android:name="android.intent.action.BOOT_COMPLETED" /> and just restart the alarm every time the device did a reboot

But both ways seem unreliable. Wouldn't that reset the alarm trigger time every single time? E.g. upon setting up the alarm it reads 3 days from the database, but then gets restarted and reads 3 again, so it gets pushed forward constantly.

Do I really have to do stuff like save current time distance in SharedPreferences and then read it out every time or so?

What would be the optimal solution for my use case?

Big_Chair
  • 960
  • 2
  • 14
  • 40
  • Switch from ALarmManager to JobScheduler, which will handle the persistant case for you. – Gabe Sechan May 31 '19 at 19:30
  • How would it again read 3 days? You used a `Calendar` instance to set values and register an alarm. When the device restarts, you need to reset alarms via a service(foreground for API 26 and higher). All will be done via broadcast receiver as you mentioned in the post. Create a separate table for alarms, store your `Calendar` instance values in the table, on reboot restore alarms by reading from that table. – Taseer Jun 01 '19 at 02:34
  • @TaseerAhmad I don't have fixed dates in the table but rather the time differences between the current and next notification, so it would have an int value of e.g. 3. Do you see my problem? – Big_Chair Jun 01 '19 at 12:36

0 Answers0