我正在使用CalendarContract插入自定义本地(非同步)日历和事件。它适用于所有设备,但不适用于Sony Xperia(4.4.4)。插入新本地日历的代码
final ContentValues cv = new ContentValues();
cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(CalendarContract.Calendars.ACCOUNT_NAME, calendarName);
cv.put(CalendarContract.Calendars.NAME, calendarName);
cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, calendarDisplayName);
cv.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xeeff0000);
cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
cv.put(CalendarContract.Calendars.VISIBLE, 1);
//cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
Uri calUri = CalendarContract.Calendars.CONTENT_URI
.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, calendarName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
//context.getContentResolver().insert(calUri, cv);
try {
Uri result = context.getContentResolver().insert(calUri, cv);
Long calId = Long.parseLong(result.getLastPathSegment());
Log.d(MainActivity.DEBUG_KEY, "==> add Calendar OK: " + calId);
return calId;
} catch (Exception exception) {
Log.d(MainActivity.DEBUG_KEY, "==> add Calendar FAILED: " + exception.getMessage());
}
calendarName是应用包名称。插入事件的代码:
long eventID = 0;
TimeZone timeZone = TimeZone.getDefault();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startDate.getTime());
values.put(CalendarContract.Events.DTEND, endDate.getTime());
values.put(CalendarContract.Events.ALL_DAY, allDay ? 1 : 0);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
try {
Uri uri = context.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);
eventID = Long.parseLong(uri.getLastPathSegment());
Log.d(DEBUG_KEY, "EVENT ADDED: " + eventID);
} catch (Exception exception) {
Log.d(DEBUG_KEY, "EVENT-ADDED FAILED: " + exception.getMessage());
}
除了异常没有错误。调试控制台显示正确插入的日历和事件ID。在所有设备上,我都可以在日历应用中查看,显示和编辑事件。
在Sony Xperia设备上,事件显示在日历应用中。但是,通过点击事件进行显示/编辑,日历应用程序会出现以下错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.android.calendar / com.android.calendar.EventInfoActivity}:java.lang.NullPointerException ... 读取此处不支持的NULL字符串。