AlarmManager that repeat at specific time

1.6k Views Asked by At

With AlarmManager you can set an alarm at specific time, and repeat it at specified interval of time

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,  
            calendar.getTimeInMillis(), 4*60*60*1000, alarmIntent);

In the example the alarm is repeating every 4 hours, but is it possible to set an alarm that trigger at specific time and repeat at specific times? For example: I want to set an alarm that start a service at 9:00 and repeat it at 11:30, 17:30 and 20:00. The service must start all days approximately at these hours.

Any idea?

3

There are 3 best solutions below

0
On BEST ANSWER

why not this way?

long now     = calendar.getTimeInMillis();
long time24h = 24*60*60*1000;
long timeAt09_00 = ...; // calculate from now...
long timeAt11_30 = ...; // calculate from now...

alarmMgr1.setInexactRepeating(AlarmManager.RTC_WAKEUP, now ,        time24h, alarmIntent);
alarmMgr2.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeAt09_00, time24h, alarmIntent);
alarmMgr3.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeAt11_30, time24h, alarmIntent);
0
On

Use the AlarmManager.setRepeating() method.

See AlarmManager Repeat for an example.

Here is the Android documentation also regarding Scheduling Repeating Alarms, with explanation and sample code.

2
On

You could create 4 alarms which starts at 9:00, 11:30, 17:30 and 20:00 with AlarmManager.setRepeating() and set repeating interval to be 24 hours (=24*60*60*1000)