3

我正在制作一个访问 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" />

会是什么呢?

4

2 回答 2

0

http://developer.android.com/guide/topics/providers/calendar-provider.html#intent-insert 将 Events.DTSTART,Events.DTEND 分别替换为 CalendarContract.EXTRA_EVENT_BEGIN_TIME,CalendarContract.EXTRA_EVENT_END_TIME。

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);
于 2013-10-29T07:12:41.813 回答
0

检查您创建的事件的日历 ID 是否指向现有的启用日历(不仅仅是 eventValues.put(Events.CALENDAR_ID, 1);)。我在尝试添加指向事件的提醒 (reminderValues.put("event_id", eventID);) 时遇到了这样的异常,该提醒是通过不与现有日历关联的 calendar_id 添加的。可能有一些检查它......

于 2016-03-15T13:40:48.527 回答