我正在创建一个访问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" />
它可能是什么?
答案 0 :(得分:0)
http://developer.android.com/guide/topics/providers/calendar-provider.html#intent-insert 分别用CalendarContract.EXTRA_EVENT_BEGIN_TIME,CalendarContract.EXTRA_EVENT_END_TIME替换Events.DTSTART,Events.DTEND。
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 0, 19, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 0, 19, 8, 30);
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
.putExtra(Events.TITLE, "Yoga")
.putExtra(Events.DESCRIPTION, "Group class")
.putExtra(Events.EVENT_LOCATION, "The gym")
.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
.putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");
startActivity(intent);
答案 1 :(得分:0)
检查您创建的事件的日历ID是否指向现有的已启用日历(而不仅仅是eventValues.put(Events.CALENDAR_ID,1);)。尝试添加指向事件的提醒时,我遇到了这样的异常(reminderValues.put(“event_id”,eventID);),这是在与现有日历无关的calendar_id中添加的。可能有一些检查......