Search code examples
androidandroid-alarms

Alarms design practice


I have two questions about alarms in android. My app sets alarms using alarm manager as requested by user

1- If my app is update (download an update from google play), will the alarms be lost and I have to reschedule them? Or they will still be triggered?

2- Upon my alarm expiry, I want to display Foo object to the screen. Currently, I have this Foo object serialized and passed as an intent extra with the alarm. is this Ok? Or should I pass the id, and upon the expiry extract that ID, call my SqlDatabase and construct the object accordingly?

Please help thanks


Solution

  • When the Alarm is very critical for the application, I usually check its existence (and create it if necessary) in the following events:

    • When device boots (android.intent.action.BOOT_COMPLETED)
    • When the app starts
    • When the app is updated (android.intent.action.PACKAGE_REPLACED), where I reset the alarms (cancel and re-create).

    I cannot recall the exact reason, but I had a bug before with Alarms that occurs when the app gets updated, that's why I am listening for this and reset the Alarms.

    I save Alarm information in SharedPreferences, so I can easily retrieve it and recreate the Alarm on crash.

    For your second question, I think passing the id only is more efficient in terms of resources, but it depends on how frequently your Alarm will fire.