How to set multiple alarms using alarm manager in android

Hassy31 picture Hassy31 · Dec 12, 2011 · Viewed 61.5k times · Source

I'm building an alarm application. I have successfully implemented basic alarm functions.

Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, sHour);
calendar.set(calendar.MINUTE, sMin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
long sdl = calendar.getTimeInMillis();

Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmList.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager ALARM1 = (AlarmManager)getSystemService(ALARM_SERVICE);
ALARM1.set(AlarmManager.RTC_WAKEUP, sdl, sender);

In my application, user can select days (sunday,monday...) to repeat the alarm weekly. I'm trying to create multiple alarms to repeat weekly but don't know how to do it. Can I create it using (repeat) interval or should I create multiple alarm managers?

Answer

Parag Chauhan picture Parag Chauhan · Apr 10, 2012

You need to use different Broadcast id's for the pending intents. Something like this:

Intent intent = new Intent(load.this, AlarmReceiver.class);
final int id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_ONE_SHOT);

Using the system time should be a unique identifier for every pending intent you fire.