我正在制作一个访问 android 日历并创建事件的应用程序。
它在 android 2.3 中工作正常,但在 4.0 > 它不会创建事件并引发异常
我不知道我做错了什么,这是我的代码:
public void addReminder() {
Calendar beginTime = Calendar.getInstance();
long startMillis = beginTime.getTimeInMillis();
long endMillis = beginTime.getTimeInMillis() + 60 * 60 * 1000;
String eventUriString = "content://com.android.calendar/events";
ContentValues eventValues = new ContentValues();
eventValues.put(Events.CALENDAR_ID, 1);
eventValues.put(Events.TITLE, "Test");
eventValues.put(Events.DESCRIPTION, "Test");
eventValues.put(Events.EVENT_TIMEZONE, "Montevideo");
eventValues.put(Events.DTSTART, startMillis);
eventValues.put(Events.DTEND, endMillis);
eventValues.put("eventStatus", 1);
eventValues.put(Events.HAS_ALARM, 1);
Uri eventUri = getContentResolver().insert(
Uri.parse(eventUriString), eventValues);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
String reminderUriString = "content://com.android.calendar/reminders";
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", eventID);
reminderValues.put("minutes", 1);
reminderValues.put("method", 1);
Uri reminderUri = getContentResolver().insert(
Uri.parse(reminderUriString), reminderValues);
}
在 android 4.0 > 这就是我得到的:
09-14 14:39:12.118: E/AndroidRuntime(826): FATAL EXCEPTION: main
09-14 14:39:12.118: E/AndroidRuntime(826): android.database.sqlite.SQLiteException
09-14 14:39:12.118: E/AndroidRuntime(826): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.content.ContentResolver.insert(ContentResolver.java:866)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.example.notificationtest.MainActivity.addReminder(MainActivity.java:131)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.example.notificationtest.MainActivity$2.onClick(MainActivity.java:68)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.view.View.performClick(View.java:4204)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.view.View$PerformClick.run(View.java:17355)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.os.Handler.handleCallback(Handler.java:725)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.os.Looper.loop(Looper.java:137)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 14:39:12.118: E/AndroidRuntime(826): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 14:39:12.118: E/AndroidRuntime(826): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 14:39:12.118: E/AndroidRuntime(826): at dalvik.system.NativeStart.main(Native Method)
这是我的权限:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
会是什么呢?