2

您好我正在尝试创建一个将事件添加到日历的应用程序。例如,我需要在每个星期六创建一个事件,直到 12 月 31 日。以下是我为创建事件设置的属性,

event.put(CalendarContract.Events.CALENDAR_ID, 1);
        event.put(CalendarContract.Events.TITLE, title);
        event.put(CalendarContract.Events.DESCRIPTION, description);
        event.put(CalendarContract.Events.EVENT_LOCATION, location);
        event.put(CalendarContract.Events.DTSTART, sDate);
        event.put(CalendarContract.Events.DURATION,"P50S");
        event.put(CalendarContract.Events.ALL_DAY, 0);
        event.put(CalendarContract.Events.HAS_ALARM, hasAlarm);
        event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
event.put(CalendarContract.Events.RRULE, "FREQ=WEEKLY;BYDAY=SA;UNTIL=20151230");
mContext.getContentResolver().insert(baseUri, event);

但它会为给定日期 (sDate) 创建一个事件,然后在每个星期六创建。但是我怎样才能避免在给定日期(sDate)创建的一个事件

4

1 回答 1

0

我有同样的问题。您需要检查每周的重复规则并将 DTSTART 偏移到最近的星期六(或重复规则包含的任何其他工作日)。为了给你一个粗略的例子,我将附加来自 Android 日历应用程序的代码,该代码根据重复规则字符串偏移事件的开始时间和结束时间,并返回两个长值 - 如果偏移量是新的开始时间和新的结束时间已应用,否则为 null。EventRecurrence 类可以通过在 GrepCode 上的搜索找到,它是 Android 日历应用程序的一部分

public static long[] offsetStartTimeIfNecessary(long startMilis, long endMilis, String rrule) {
    if (rrule == null || rrule.isEmpty() || rrule.replace("RRULE:", "").isEmpty()) {
        // No need to waste any time with the parsing if the rule is empty.
        return null;
    }
    long result[] = new long[2];
    Calendar startTime = Calendar.getInstance();
    startTime.setTimeInMillis(startMilis);
    Calendar endTime = Calendar.getInstance();
    endTime.setTimeInMillis(endMilis);
    EventRecurrence mEventRecurrence = new EventRecurrence();
    mEventRecurrence.parse(rrule.replace("RRULE:", ""));
    // Check if we meet the specific special case. It has to:
    //  * be weekly
    //  * not recur on the same day of the week that the startTime falls on
    // In this case, we'll need to push the start time to fall on the first day of the week
    // that is part of the recurrence.
    if (mEventRecurrence.freq != EventRecurrence.WEEKLY) {
        // Not weekly so nothing to worry about.
        return null;
    }
    if (mEventRecurrence.byday == null ||
            mEventRecurrence.byday.length > mEventRecurrence.bydayCount) {
        // This shouldn't happen, but just in case something is weird about the recurrence.
        return null;
    }
    // Start to figure out what the nearest weekday is.
    int closestWeekday = Integer.MAX_VALUE;
    int weekstart = EventRecurrence.day2TimeDay(mEventRecurrence.wkst);
    int startDay = startTime.get(Calendar.DAY_OF_WEEK) - 1;
    for (int i = 0; i < mEventRecurrence.bydayCount; i++) {
        int day = EventRecurrence.day2TimeDay(mEventRecurrence.byday[i]);
        if (day == startDay) {
            // Our start day is one of the recurring days, so we're good.
            return null;
        }
        if (day < weekstart) {
            // Let's not make any assumptions about what weekstart can be.
            day += 7;
        }
        // We either want the earliest day that is later in the week than startDay ...
        if (day > startDay && (day < closestWeekday || closestWeekday < startDay)) {
            closestWeekday = day;
        }
        // ... or if there are no days later than startDay, we want the earliest day that is
        // earlier in the week than startDay.
        if (closestWeekday == Integer.MAX_VALUE || closestWeekday < startDay) {
            // We haven't found a day that's later in the week than startDay yet.
            if (day < closestWeekday) {
                closestWeekday = day;
            }
        }
    }
    if (closestWeekday < startDay) {
        closestWeekday += 7;
    }
    int daysOffset = closestWeekday - startDay;
    startTime.add(Calendar.DAY_OF_MONTH, daysOffset);
    endTime.add(Calendar.DAY_OF_MONTH, daysOffset);
    result[0] = startTime.getTimeInMillis();
    result[1] = endTime.getTimeInMillis();
    return result;
}
于 2015-11-17T11:06:43.920 回答