I am retreiving a list of dates from HTML.
The list is set to a List when it is retreived.
I want to use a SimpleDate Parser(which i know how to do) to parse and set the date for the AlarmManager.
Since it is more than one Date. How could i create a method for this? To create a Alarm for each Date.开发者_开发知识库
The first time the list of dates is loaded i would like it to parse the dates and set an alarm for each.
And if its more than one alarm on one day, how could i combine them as one?
You can set the alarm as you contain the size of the List object which contains the date.
now parse it and set in alarm with the pending object in any loop like for loop.
just use the same object for the Intent, PendingIntent AlarmManager
in the for loop do like this way
Calendar setAlarm=Calendar.getInstance();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
for(int i=0;i<listDate.size();i++){
Intent intent=new Intent(getApplicationContext(),"YOUR BROADCAST CLASS");
PendingIntent sender = PendingIntent.getBroadcast(this,i, intent, PendingIntent.FLAG_ONE_SHOT);
/// if you parse date direct then here pass
setAlarm.setTime(sdf.parse(listDate.get(0));
am.set(AlarmManager.RTC_WAKEUP, setAlarm.getTimeInMillis(), sender);
}
Set an alarm for each, it is possible and supported. However, please note that you have to differentiate the alarms in such a way so that Android won't "overwrite" the old ones. The thing is that if 2 alarms use the same intent params and extras the new one will overwrite the old one.
For example, you should change the requestId param like this:
Intent intent = new Intent(context, Alarm.class);
intent.putExtra("id", String.valueOf(**uniqueRequestId**));
sender = PendingIntent.getBroadcast(context, **uniqueRequestId**, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, sender);
精彩评论