无法在Marshmallow设备上添加日历事件的创建提醒

时间:2016-10-16 17:48:27

标签: android

有人可以帮助我以编程方式为日历活动创建提醒。它在API级别22上完全正常,但在23级(Marshmallow)上没有。

代码

ContentValues reminderValues = new ContentValues();
reminderValues.put(CalendarContract.Reminders.EVENT_ID, 1);
reminderValues.put(CalendarContract.Reminders.MINUTES, 1); 
reminderValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_DEFAULT);
Uri reminderUri = getApplicationContext().getContentResolver()
                    .insert(CalendarContract.Reminders.CONTENT_URI, reminderValues);

异常

  

SqliteDoneException

1 个答案:

答案 0 :(得分:1)

private int getPrimaryCalendar() {
    // noinspection ResourceType
    Cursor managedCursor = getContentResolver().query(CalendarContract.Calendars.CONTENT_URI, new String[]{
            CalendarContract.Calendars._ID, CalendarContract.Calendars.IS_PRIMARY}, null, null, null);

    int calID = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (managedCursor != null && managedCursor.moveToFirst()) {
            do {
                calID = managedCursor.getInt(managedCursor.getColumnIndex(CalendarContract.Calendars._ID));
                int columnIndex = -1;
                try {
                    columnIndex = managedCursor.getColumnIndex(CalendarContract.Calendars.IS_PRIMARY);
                } catch (NullPointerException e) {
                    LogUtil.d(TAG, e.getMessage());
                }
                if (columnIndex != -1 && managedCursor.getInt(columnIndex) == 1) {
                    break;
                } else {
                    calID = 1;
                }
            } while (managedCursor.moveToNext());
            managedCursor.close();
        }
    }

    return calID;
}
  

使用默认日历