Motassem MK Motassem MK - 8 months ago 31
Java Question

Multiple Dynamic Time Event Listener

This solution for time event listeners is awesome:
Time Event Listener

Here's a code sample:

Calendar cal = Calendar.getInstance();
Intent activate = new Intent(context, Alarm.class);
AlarmManager alarms ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, activate, 0);
alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 00);
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);

How can I make many of these dynamically? I have a for loop with
int i
containing all hours that the user wants to set alarms to. I thought of creating dynamic instances of Calendar (cal+i), but we don't have it in Java.


Thank you all, the solution was by repeating the code and using i as a request code in the pendingIntent like this :

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, i, activate, 0);