我面临着奇怪的问题。
我正在尝试添加事件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());
但我无法在我的日历应用程序中看到此日历。当我尝试使用这个新日历添加事件时。事件得到显示,但单击事件时,我的默认日历应用程序崩溃了。将新日历添加到默认日历应用程序时我做错了什么?谢谢。