在我的日历应用程序中,我无法编辑Android 2.x的日历事件,但它适用于4个版本。
这是我的内容uri,当我为版本2.x添加事件时可以使用,但在编辑我的事件时会出现异常。
eventUriString = "content://com.android.calendar/events";
这是我的方法:
public static long editAppointmentsToCalender(Context curActivity,int calenderId, String title,
String addInfo, String place, int status, long startDateTime, long endDateTime, boolean needReminder,
boolean needMailService, int repeat,String[] selArgs,String[] selArgs2) {
String eventUriString = null;
ContentValues eventValues = new ContentValues();
if(Build.VERSION.SDK_INT >= 8 && Build.VERSION.SDK_INT <= 14){
eventUriString = "content://com.android.calendar/events";
eventValues.put("calendar_id", calenderId);
eventValues.put("title", title);
eventValues.put("description", addInfo);
eventValues.put("eventLocation", place);
eventValues.put("dtstart", startDateTime);
eventValues.put("dtend", endDateTime);
eventValues.put("rrule","FREQ=DAILY;INTERVAL=2");
eventValues.put("eventStatus", status);
eventValues.put("visibility", 3);
eventValues.put("transparency", 0);
eventValues.put("hasAlarm", 1);
eventValues.put("eventTimezone", Time.getCurrentTimezone());
eventID = curActivity.getContentResolver().update(Uri.parse(eventUriString), eventValues,"event_id =? ", selArgs);
System.out.println("is updated===>"+eventID);
}else if(Build.VERSION.SDK_INT >= 15){
eventUriString = CalendarContract.Events.CONTENT_URI.toString();
eventValues.put(CalendarContract.Events.CALENDAR_ID, calenderId);
eventValues.put(CalendarContract.Events.TITLE, title);
eventValues.put(CalendarContract.Events.DESCRIPTION, addInfo);
eventValues.put(CalendarContract.Events.EVENT_LOCATION, place);
eventValues.put(CalendarContract.Events.DTSTART, startDateTime);
eventValues.put(CalendarContract.Events.DTEND, endDateTime);
eventValues.put(CalendarContract.Events.RRULE, "FREQ=DAILY;INTERVAL=1");
eventValues.put(CalendarContract.Events.STATUS, status);
eventValues.put(CalendarContract.Events.HAS_ALARM, 1);
TimeZone timeZone = TimeZone.getDefault();
eventValues.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
eventID = curActivity.getContentResolver().update(Uri.parse(eventUriString), eventValues,Events._ID + " =? ", selArgs);
System.out.println("is updated===>"+eventID);
}else{
eventUriString = "content://calendar/calendars";
eventValues.put("calendar_id", calenderId);
eventValues.put("title", title);
eventValues.put("description", addInfo);
eventValues.put("eventLocation", place);
eventValues.put("dtstart", startDateTime);
eventValues.put("dtend", endDateTime);
eventValues.put("eventStatus", status);
eventValues.put("visibility", 3);
eventValues.put("transparency", 0);
eventValues.put("hasAlarm", 1);
eventID = curActivity.getContentResolver().update(Uri.parse(eventUriString), eventValues,"event_id =? ", selArgs);
System.out.println("is updated===>"+eventID);
}
错误日志:
11-30 19:52:11.117: E/AndroidRuntime(20457): FATAL EXCEPTION: main
11-30 19:52:11.117: E/AndroidRuntime(20457): java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/events
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.content.ContentProviderProxy.update(ContentProviderNative.java:510)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.content.ContentResolver.update(ContentResolver.java:724)
11-30 19:52:11.117: E/AndroidRuntime(20457): at com.example.calendar.CreateEvent.editAppointmentsToCalender(CreateEvent.java:129)
11-30 19:52:11.117: E/AndroidRuntime(20457): at com.example.calendar.CreateEventActivity$3.onClick(CreateEventActivity.java:110)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.view.View.performClick(View.java:2485)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.view.View$PerformClick.run(View.java:9080)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.os.Handler.handleCallback(Handler.java:587)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.os.Looper.loop(Looper.java:130)
11-30 19:52:11.117: E/AndroidRuntime(20457): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-30 19:52:11.117: E/AndroidRuntime(20457): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 19:52:11.117: E/AndroidRuntime(20457): at java.lang.reflect.Method.invoke(Method.java:507)
11-30 19:52:11.117: E/AndroidRuntime(20457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-30 19:52:11.117: E/AndroidRuntime(20457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-30 19:52:11.117: E/AndroidRuntime(20457): at dalvik.system.NativeStart.main(Native Method)