Motassem MK Motassem MK - 1 year ago 73
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.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download