我用作指南:
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)
我对该活动的看法:
我需要什么,考虑到:
我需要添加一个Android事件,每天从11.06到14.06持续1小时(不包括在内 - 因为开始时间高于重复结束时间):
我到目前为止所做的事情(忽略非经常性事件 - 在这些情况下一切正常运作):
...
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());
...
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";
}
会发生什么:没有,没有错误。但事件并未在日历中显示。同样,对于非经常性的,它按预期工作