添加事件和提醒在 6.0 marshmallow 中不起作用

2024-07-03

我面临着奇怪的问题。 我正在尝试添加事件calendar并且reminder。除了具有以下功能的设备外,它在所有设备上都能正常工作Marshmallow - 6.0

当我尝试添加事件时,它还会在此处返回事件 ID。即使我在这里获取事件 ID,I can not see events added in my calendar

   ContentValues event = new ContentValues();
        event.put("calendar_id", 1);
        event.put("title", "Match");
        event.put("description",PTGConstantMethod.validateString(match.getMatch_name()));
        event.put("eventLocation", match.getVenue());
        event.put("eventTimezone", TimeZone.getDefault().getID());
        event.put("dtstart", startDate.getTime());
        event.put("dtend", endDate);

        event.put("allDay", 0); // 0 for false, 1 for true
        event.put("eventStatus", 1);
        event.put("hasAlarm", 1); // 0 for false, 1 for true

        Uri eventUri = context.getApplicationContext()
                .getContentResolver()
                .insert(Events.CONTENT_URI, event);
        long eventID = Long.parseLong(eventUri.getLastPathSegment());

我在这里添加提醒,

        ContentValues reminders = new ContentValues();
            reminders.put("event_id", eventID);
            reminders.put("method", CalendarContract.Reminders.METHOD_ALERT);
            reminders.put("minutes", minutes);

            context.getApplicationContext().getContentResolver()
                    .insert(Reminders.CONTENT_URI, reminders);

但应用程序在添加提醒时崩溃。这是我的日志。

android.database.sqlite.SQLiteException 在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179) 在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 在 android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) 在 android.content.ContentResolver.insert(ContentResolver.java:1231)

在我的 build.gradle 中

  minSdkVersion 14
  targetSdkVersion 22

终于发现没有日历了event.put("calendar_id", 1);。那么我怎样才能获得应用程序默认日历呢?

我尝试添加新日历并向其中添加事件。

        ContentValues calendar = new ContentValues();
    calendar.put(CalendarContract.Calendars.NAME, context.getResources().getString(R.string.app_name));
    calendar.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, context.getResources().getString(R.string.app_name)+" Calendar");
    calendar.put(CalendarContract.Calendars.VISIBLE, true);

    Uri uri = context.getApplicationContext()
            .getContentResolver()
            .insert(CalendarContract.Calendars.CONTENT_URI, calendar);
    long calendarId = Long.parseLong(uri.getLastPathSegment());

但我无法在我的日历应用程序中看到此日历。当我尝试使用这个新日历添加事件时。事件得到显示,但单击事件时,我的默认日历应用程序崩溃了。将新日历添加到默认日历应用程序时我做错了什么?谢谢。


抱歉以这种方式发布答案,但是我在 Android M 日历中手动添加事件时发现,它说我们需要将帐户添加到日历帐户才能添加事件。因此,我猜测,除非且直到我们不将日历添加或映射到至少一个帐户,否则我们无法手动或以编程方式将事件添加到 Android M 设备日历。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

添加事件和提醒在 6.0 marshmallow 中不起作用 的相关文章

随机推荐