Add event and reminder not working in 6.0 marshmal

2020-07-23 06:44发布

问题:

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.

回答1:

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.



回答2:

Try below link:

How to add multiple event to android marshmallow Calendar?



回答3:

Maybe you want to use inside Fragment.

At first use:

super.requestPermissions( new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR);

Inside fragment, you need to call:

FragmentCompat.requestPermissions(permissionsList, RequestCode)

Note:

ActivityCompat.requestPermissions(Activity, permissionsList, RequestCode);

add this library in gradle for FragmentCompat class .

compile 'com.android.support:support-v13:version_of_library'