0

我目前正在创建一个自定义日历应用程序,并且一直在努力保存重复发生的事件。例如,如果我将事件设置为每天发生 100 天,我只会看到大约 38 个实例保存到实例数据库中。不知道为什么会这样。

这是添加/更新事件的代码

public Event addUpdateCalendarEvent(Context ctx, Event event) throws InterruptedException {

    ContentResolver contentResolver = ctx.getContentResolver();

    ContentValues calEvent = new ContentValues();
    calEvent.put(Events.CALENDAR_ID, event.getCalendarId());

    calEvent.put(Events.TITLE, event.getEventName());
    calEvent.put(Events.DTSTART, event.getDateFrom().getTimeInMillis()); 
    calEvent.put(Events.EVENT_TIMEZONE,event.getDateFrom().getTimeZone().getID());
    if(event.isAllDay()) {
        calEvent.put(Events.ALL_DAY, 1);
    }
    if(null != event.getRrule()) {
        String rrule = RecurrenceRuleConverter.toString(event.getRrule());
        Log.d("rrule: ", rrule);
        calEvent.put(Events.RRULE, rrule);
    }
    if(null != event.getDuration()) {
        calEvent.put(Events.DURATION, event.getDuration());
    }
    if(null != event.getDateTo()) {
        calEvent.put(Events.DTEND, event.getDateTo().getTimeInMillis());
    }
    Uri uri = null;
    try {
         if(null != event.getEventId()) {
            uri = ContentUris.withAppendedId(Events.CONTENT_URI, event.getEventId());
            contentResolver.update(uri, calEvent, null, null);
        }
        else {
            uri = contentResolver.insert(Events.CONTENT_URI, calEvent);
        }
    }
    finally {
        event.setEventId(Integer.parseInt(uri.getLastPathSegment()));
    }
    return event;
}

在我调用 addUpdate 方法后,我立即调用另一个方法来列出实例

public List<Event> getEventInstances(Context context, Integer calendarId, Integer eventId) {
    // Projection array. Creating indices for this array instead of doing
    // dynamic lookups improves performance.
    final String[] EVENT_PROJECTION = new String[] {
        CalendarContract.Instances.CALENDAR_ID,
        CalendarContract.Instances._ID, 
        CalendarContract.Instances.BEGIN, 
        CalendarContract.Instances.END,
        CalendarContract.Instances.TITLE,
        CalendarContract.Instances.RRULE,
        CalendarContract.Instances.EVENT_ID,
        CalendarContract.Instances.EVENT_TIMEZONE,
        CalendarContract.Instances.ALL_DAY
    };

    // Run query
    Cursor cur = null;
    ContentResolver cr = context.getContentResolver();


    Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); 
    ContentUris.appendId(builder, Long.MIN_VALUE);
    ContentUris.appendId(builder, Long.MAX_VALUE); 
    Uri uri = builder.build();
    List<Event> events = new ArrayList<Event>();
    String selection = "((" + Instances.CALENDAR_ID + " = ?) AND (" + Instances.EVENT_ID + " = ?))";
    String[] selectionArgs = new String[] {String.valueOf(calendarId),String.valueOf(eventId)}; 
    try {
        cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);

        while (cur.moveToNext()) {
            Event event = new Event();
            event.setCalendarId(cur.getInt(0));
            java.util.Calendar calFrom = java.util.Calendar.getInstance();
            calFrom.setTimeInMillis(Long.valueOf(cur.getLong(2)));
            java.util.Calendar calTo = java.util.Calendar.getInstance();
            calTo.setTimeInMillis(Long.valueOf(Long.valueOf(cur.getLong(3))));
            event.setDateFrom(calFrom);
            event.setDateTo(calTo);
            event.setTitle(cur.getString(4));
            event.setEventId(cur.getInt(6));
            RecurrenceRule rule = new RecurrenceRule();
            rule.setRuleAsText(cur.getString(5));
            event.setRrule(rule);  
            event.setAllDay(cur.getInt(8) == 1);
            Log.d("Event:", event.getTitle() + " CalendarID: "  + event.getCalendarId() + " EventId: " + event.getEventId() + " " + DateUtil.dateTimeFormat.format(calFrom.getTime()) + " to " + DateUtil.dateTimeFormat.format(calTo.getTime()) + " TimeZone: " + cur.getString(7));

            events.add(event);
        }
    }
    finally {
        cur.close();
        sortListEventsByDate(events);
        Log.d("GetEventInstances Size", String.valueOf(events.size()));

    }
    return events;
}

以下是我尝试保存的一个事件的属性示例,该事件持续了 100 天:

event   Event  (id=830026469328)    
calendarId  Integer  (id=830015046320)  
dateFrom    GregorianCalendar  (id=830027217712)    
dateTo  null    
duration    "P1H" (id=830029516016) 
eventId Integer  (id=830029520232)  
eventName   "ginos pizza" (id=830044532096) 
eventTimezone   "Central Standard Time" (id=830024050504)   
isAllDay    false   
rdate   null    
rrule   ruleAsText  "FREQ=DAILY;COUNT=100" (id=830029519256)    

以下是来自 vcd 文件的事件信息:

BEGIN:VCALENDAR
VERSION:1.0
PRODID:vCal ID default
TZ:-05:00
BEGIN:VEVENT
UID:content://com.android.calendar/events/1187
DTEND:20130810T040000Z
DTSTART:20130810T030000Z
DUE:P3600S
COMPLETED:20131117T050000Z
RRULE:FREQ=DAILY;COUNT=100
SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:=67=69=6E=6F=73=20=70=69=7A=7A=61
STATUS:TENTATIVE
END:VEVENT
END:VCALENDAR

无法弄清楚为什么它只保存了一小部分实例。请帮忙!!!如果您需要查看任何其他信息,请告诉我。

谢谢你,亚伦

4

1 回答 1

0

我前一阵子想通了……对不起,我没有早点添加。问题在于在搜索事件时传入 Long.min 和 Long.max。我通过一次加载事件一个月解决了这些问题。

于 2013-11-06T01:35:55.873 回答