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);
そしてそれは私の電話(Android2.3.7を搭載したSEX8)で動作しますが、SDK14では動作しません。
カレンダーにイベントを追加するために使用できるユニバーサルコードはどこにありますかAndroidはさまざまなSDKで動作しますか?私はそれがどのように作られるのか分かりません。マネージャーが望んでいるので、API7を使用する必要があります。どのようにそれを行うかについて何かアイデアはありますか?