Android日历事件未显示

时间:2015-06-30 19:34:26

标签: android calendar calendarcontract

您好我有以下代码在我的日历上添加活动:

public String addCalendarEntry(CalendarDTO calendar) {

        ContentValues event = new ContentValues();
        ContentResolver cr = getContentResolver();

        long startMillis = calendar.getStartDate().getTimeInMillis();
        long endMillis = calendar.getEndDate().getTimeInMillis();
        String timeZone = TimeZone.getDefault().getID();

        event.put(CalendarContract.Events.CALENDAR_ID, calendar.getId());
        event.put(CalendarContract.Events.TITLE, calendar.getTitle());
        event.put(CalendarContract.Events.DESCRIPTION, calendar.getDescription());
        event.put(CalendarContract.Events.EVENT_LOCATION, calendar.getLocation());
        event.put(CalendarContract.Events.DTSTART, startMillis);
        event.put(CalendarContract.Events.DTEND, endMillis);
        event.put(CalendarContract.Events.ALL_DAY, Boolean.getBoolean(String.valueOf(calendar.isAllDay())));   // 0 for false, 1 for true
        event.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true
        event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);

        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, event);

        return uri.getLastPathSegment();
}

此代码工作正常,直到前一天,并且突然间不再向我的日历中添加任何事件。它也没有错误,所以我在这里失去了。

CalendarDTO只是一个数据传输类:

public class CalendarDTO {
    int id;
    String title;
    String description;
    String location;
    Calendar startDate;
    Calendar endDate;
    boolean isAllDay;

// getters and setters removed for brevity
}

任何线索都受到高度赞赏。

1 个答案:

答案 0 :(得分:6)

您是否检查过calendar.getId()的日历?

来自文档:http://developer.android.com/reference/android/provider/CalendarContract.Events.html

  

写入事件   “事件”表中的所有“更新”和“插入”还有其他限制:

     

如果allDay设置为1,则eventTimezone必须为TIMEZONE_UTC且时间必须对应于午夜边界。

尝试这样的事情:

event.put(CalendarContract.Events.CALENDAR_ID, calendar.getId());
    event.put(CalendarContract.Events.TITLE, calendar.getTitle());
    event.put(CalendarContract.Events.DESCRIPTION, calendar.getDescription());
    event.put(CalendarContract.Events.EVENT_LOCATION, calendar.getLocation());
    if(calendar.isAllDay){
        Calendar start=calendar.getStartDate();
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);
        Calendar end=start;
        end.add(Calendar.DAY_OF_MONTH,1);
        event.put(CalendarContract.Events.DTSTART, start.getTimeInMillis());
        event.put(CalendarContract.Events.DTEND, end.getTimeInMillis());
        event.put(CalendarContract.Events.EVENT_TIMEZONE, Time.TIMEZONE_UTC);
    }else{
        event.put(CalendarContract.Events.DTSTART, startMillis);
        event.put(CalendarContract.Events.DTEND, endMillis);
        event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
    }
    event.put(CalendarContract.Events.HAS_ALARM, 1);
    event.put(CalendarContract.Events.ALL_DAY, calendar.isAllDay()?1:0);