I am facing on weird issue.
I am trying to add events in calendar
and also reminder
. It's working fine in all devices except the one having Marshmallow - 6.0
When i try to add events, it also returns me event id here. And even though i am getting event id here, 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());
I am adding reminder here,
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);
But app crashes while adding reminder. Here are my logs.
android.database.sqlite.SQLiteException at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) at android.content.ContentResolver.insert(ContentResolver.java:1231)
In my build.gradle
minSdkVersion 14
targetSdkVersion 22
I finally found that there isn't any calendar with event.put("calendar_id", 1);
. So how could i get app default calendar?
I tried by adding new calendar and add events to that.
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());
But i am not able to see this calendar in my calendar app. When i try to add events using this new calendar. Events get display but on click of event, my default calendar app gets crashed. What i am doing wrong while adding new calendar to default calendar app? Thanks.
Sorry for posting answer in such a way,but what I found while adding event manually in Android M calendar, it says that we need to add account to calendar account in order to add events. So I am guessing that unless and until we do not add or map a calendar to at least one account, we cannot add events manually or programmatically to Android M device calendar.