• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Multiple alarms every day

manvinder

Newbie
Hi,

I have to set multiple alarms (upto 5) that will trigger everyday, in my application. I tried to create an array of AlarmManager instances but it didn't worked. Here is the code fragment I used for this.

[HIGH]AlarmManager[] alarmManager = new AlarmManager[totalAlarmsInDay];

for(int i = 0; i < totalAlarmsInDay; i++) {
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis() + i * 15*60*1000, AlarmManager.INTERVAL_DAY, pendingIntent);
}[/HIGH]

Thanks.
 
getSystemService will return the same instance of Context.ALARM_SERVICE every time, so having an array of them is pointless. You need only one instance of AlarmManager, then schedule multiple PendingIntents with it.
 
One other item to mention is i noticed setInexactRepeating was used. This does not guarantee an alarm will fire when you expect it to.

If you want the device to wake up after a specific time has passed, here is how you would schedule the alarm:
am.setRepeating(AlarmManager.RTC_WAKEUP, nextUpdateTime, interval, pendingIntent);

A snippet from the api:

void setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.

void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

Schedule a repeating alarm.
 
Back
Top Bottom