以频率和重复日期结束在 Android 日历中添加重复事件

use*_*048 5 java android calendar date

我用作指南的内容:

http://developer.android.com/guide/topics/providers/calendar-provider.html#events

http://developer.android.com/reference/android/provider/CalendarContract.Events.html

http://tools.ietf.org/html/rfc5545#section-3.8.5.3(用于 RRULE)

我对事件的看法:

  • 开始日期
  • 重复结束日期
  • 频率(每天/每周/每月/每年)

我需要什么,考虑:

  • 开始日期:11.06.2014 13:00
  • 重复频率:“每天”
  • 重复结束日期:14.06.2014 10:00

我需要添加一个从 11.06 到 14.06 每天持续 1 小时的 Android 事件(不包括在内 - 因为开始时间高于重复结束时间):

  • 11.06 13:00-14:00
  • 12.06 13:00-14:00
  • 13.06 13:00-14:00

到目前为止我所拥有的(忽略非重复性事件 - 在这些情况下一切都按预期工作):

...
ContentValues values = new ContentValues();
values.put(Events.DTSTART, event.getDate().getTime());
if (!event.isRecurring()) { // ignore
    values.put(Events.DTEND, event.getEndDate().getTime());
    values.putNull(Events.DURATION);
    values.putNull(Events.RRULE);
    values.putNull(Events.RDATE);
} else {
    values.putNull(Events.DTEND);
    values.put(Events.DURATION, "PT1H"); // set event duration as 1H - this is ok hardcoded
    values.put(Events.RDATE, event.getRecurringEnd().getTime()); // when the event should stop recurring
    values.put(Events.RRULE,
            Utils.getRRuleForRecurring(event.getRecurring()) + ";UNTIL=" + event.getRecurringEnd().getTime());  
}
values.put(Events.TITLE, event.getName());
values.put(Events.DESCRIPTION, event.getDescription() == null ? ""
        : event.getDescription());
values.put(Events.CALENDAR_ID, calendarId);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
...
Run Code Online (Sandbox Code Playgroud)

RRule 生成方法:

public static String getRRuleForRecurring(String recurring) {
    recurring = recurring.toLowerCase();
    if (recurring.equals("every day")) {
        return "FREQ=DAILY";
    }
    if (recurring.equals("every month")) {
        return "FREQ=MONTHLY";
    }
    if (recurring.equals("every week")) {
        return "FREQ=WEEKLY";           
    }
    if (recurring.equals("every year")) {
        return "FREQ=YEARLY";
    }

    return "FREQ=DAILY";
}
Run Code Online (Sandbox Code Playgroud)

会发生什么:什么都没有,没有错误。但事件不会显示在日历中。同样,对于非经常性的,它按预期工作