对写入重复事件到系统日历的补充(设置重复规则)

设置重复规则:

//用RFC5545格式表示的事件持续时间,例如“PT1H”表示事件持续1小时的状态, “P2W”指明2周的持续时间。
String duration = "P" + DateUtils.countSecondBetweenTwoDates(schedule.getStartTime(), schedule.getEndTime()) + "S";
int repeat = Integer.parseInt(schedule.getRepeat());  // 重复周期
 
  
switch (repeat) {
    // 每天
    case ScheduleConstants.REPEAT_CYCLE_DAILY:
        event.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL=" + until + ";WKST=SU");
        break;
    // 每周
    case ScheduleConstants.REPEAT_CYCLE_WEEKLY:
        event.put(CalendarContract.Events.RRULE, "FREQ=WEEKLY;UNTIL=" + until + ";WKST=SU");
        break;
    // 每两周
    case ScheduleConstants.REPEAT_CYCLE_TWO_WEEKLY:
        event.put(CalendarContract.Events.RRULE, "FREQ=WEEKLY;INTERVAL=2;UNTIL=" + until + ";WKST=SU");
        break;
    // 每月
    case ScheduleConstants.REPEAT_CYCLE_MONTHLY:
        event.put(CalendarContract.Events.RRULE, "FREQ=MONTHLY;UNTIL=" + until + ";WKST=SU");
        break;
    // 每年
    case ScheduleConstants.REPEAT_CYCLE_YEARLY:
        event.put(CalendarContract.Events.RRULE, "FREQ=YEARLY;UNTIL=" + until + ";WKST=SU");
        break;
    default:
        break;
}
event.put(CalendarContract.Events.DURATION, duration);
event.put(CalendarContract.Events.DTEND, (byte[]) null);

/**
 * 计算两个日期之间的秒数
 * @param startDate
 * @return 返回秒数
 */
public static int countSecondBetweenTwoDates(Date startDate,Date endTime) {
    long a =startDate.getTime();
    long b = endTime.getTime();
    int c = (int)((b - a) / 1000);
    return c;
}



你可能感兴趣的:(Android)