如何解析日期列表并将它们设置为AlarmManager?

时间:2011-08-25 12:11:47

标签: android alarmmanager

我正在从HTML中检索日期列表。

列表在被检索时设置为列表。

我想使用SimpleDate Parser(我知道该怎么做)来解析和设置AlarmManager的日期。

因为它不止一个日期。我怎么能为此创建一个方法?为每个日期创建警报。

第一次加载日期列表时,我希望它能够解析日期并为每个日期设置警报。

如果它在一天内发出多个警报,我怎么能把它们合并为一个呢?

2 个答案:

答案 0 :(得分:1)

您可以在包含包含日期的List对象的大小时设置闹钟。

现在解析它并在任何循环中使用挂起对象设置警报,如for循环。

只需为Intent,PendingIntent AlarmManager使用相同的对象

for循环中的

就是这样做的

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);
}

答案 1 :(得分:1)

为每个设置一个警报,可以并且支持。 但请注意,您必须以这种方式区分警报,以便Android不会“覆盖”旧警报。 问题是,如果2个警报使用相同的意图参数和附加信息,则新警报将覆盖旧警报。

例如,您应该像这样更改requestId参数:

        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);