在Calendar中添加重复事件时出错

时间:2015-11-09 09:52:50

标签: android events calendar android-calendar rrule

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

<xs:schema xmlns="urn:Magic xpa.printdata" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           targetNamespace="urn:Magic xpa.printdata">
<xs:element name="projectnr">
    <xs:simpleType>
        <xs:restriction base="xs:integer">
            <xs:totalDigits value="6"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

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

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。您需要检查星期几的重复规则,并将DTSTART偏移到最接近的星期六(或您的重复规则包含的任何其他工作日)。为了给您提供粗略的示例,我将附加来自Android日历应用程序的代码,该代码根据reccurence规则字符串抵消事件的开始时间和结束时间,并返回两个长值 - 如果偏移量为新的开始时间和新的结束时间应用,如果不是,则为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;
}