我在向 Android 日历添加事件时遇到问题。我的最低 SDK 版本是 7。我使用 Intent 添加事件,但问题在于各种 API。我用这个:
String eventUriString;
if (Build.VERSION.SDK_INT > 7)
eventUriString = "content://com.android.calendar/events";
else eventUriString = "content://calendar/events";
ContentValues eventValues = new ContentValues();
eventValues.put("calendar_id", 1);
eventValues.put("title", "title");
eventValues.put("description", desc);
eventValues.put("eventLocation", "");
long endDate = startDate + 1000 * 60 * 60;
eventValues.put("dtstart", MyClass.getDate());
eventValues.put("dtend", endDate);
eventValues.put("eventStatus", 0);
eventValues.put("visibility", 2);
eventValues.put("transparency", 0);
eventValues.put("hasAlarm", 0);
Uri eventUri = context.getContentResolver().insert(Uri.parse(eventUriString), eventValues);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
编辑我使用的一些事件:
String calendarUriBase = null;
long id = MyEvents.getID(p); //something ID from my another class
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
// eat
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
// eat
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
ContentValues event = new ContentValues();
event.put("title", "new Title");
Uri eventsUri = Uri.parse(calendarUriBase+"events");
Uri eventUri = ContentUris.withAppendedId(eventsUri, id);
getContentResolver().update(eventUri, event, null, null);
它适用于我的手机(带有 Android 2.3.7 的 SE X8),但不适用于 SDK 14。
是否有通用代码,我可以使用它来向日历添加事件 Android 适用于各种 SDK?我不知道它是怎么做的。我必须使用 API 7,因为我的经理想要。你有什么想法吗?