zzlalani
zzlalani

Reputation: 24344

Android: Fire all missed alarms when device turns on

In my application the user has set some reminders and I have to alarm them when the time has come. Currently I'm using AlarmManager.RTC as a type of AlarmManager

am.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);

it in working as per instruction

// Sets an alarm - note this alarm will be lost if the phone is turned
// off and on again

is there any way the I can fire missed alarms when I turn on the device?

Please note that I don't want to wakeup the device, I just wanted to remind them when they turn on the device.

PS: I have read the documentation http://developer.android.com/reference/android/app/AlarmManager.html#RTC, but I didn't find my option.

Upvotes: 0

Views: 1648

Answers (4)

paulparkbom
paulparkbom

Reputation: 21

Register broadcastReceiver in your Manifest.xml

<receiver
    android:name=".AlarmRestartReceiver"
    android:enabled="true">
    <intent-filter>
        <action 
          android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

After this, listen event when device wake up.

override fun onReceive(context: Context, intent: Intent?) {
   if (intent?.action == "android.intent.action.BOOT_COMPLETED") {
      // re-register all alarm here..
   }
}

Upvotes: 0

mfs
mfs

Reputation: 4074

AlarmManager will alarm all of your alarms till your phone is on.

Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

Now that only way fits is to kepp tracks through database. Put an additional class that extends BroadcastReceiver. Where in its OnReceive() method you can re-generate your alarms when device reboots. (obviously if you have some track, i.e database). Setting BroadcastReceiver for your purpose can be seen here How to start an Application on startup?. Hope this helps.

Upvotes: 1

JDJ
JDJ

Reputation: 4328

You would have to do this manually.

One way to do this would be to listen for the phone shutdown event:

<action android:name="android.intent.action.ACTION_SHUTDOWN" />

and before the device shuts down, save the shutdown time.

Then listen for device bootup:

<action android:name="android.intent.action.BOOT_COMPLETED" />

and write your own logic that determines which alarms would have fired during that downtime, and fire those alarms immediately with AlarmManager.

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1006539

is there any way the I can fire missed alarms when I turn on the device?

Step #1: Have some way of tracking, yourself, what is missed, such as by tracking event status in a database.

Step #2: Set up a BOOT_COMPLETED BroadcastReceiver to find when the phone is rebooted. In there, schedule any alarm(s) that are still in the future, and decide what to do about alarms that occurred in the past but were missed (e.g., raise a Notification pointing out the missed events).

Upvotes: 4

Related Questions