android – Multiple alarms with AlarmManager

Question:

How to set more than one alarm with AlarmManager ? I recently created a topic about services , and in order not to get too attached I decided to create another one:

Look at the code I'm using:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Intent tarefaIntent = new Intent(context, ExecutarTarefaProgramadaReceiver.class);
PendingIntent tarefaPendingIntent = PendingIntent.getBroadcast(context,1234, tarefaIntent,0);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

//Definir o alarme para acontecer todos os dias às 10 horas
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                 AlarmManager.INTERVAL_DAY, tarefaPendingIntent);

It has an alarm at 10 am, and a break every 1 day, what if I want 2 alarms? ex: one of 10 and one of 11, with the same interval of 1 day?

Answer:

You need to use different Transmission id for the outstanding intentions. 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);
Scroll to Top