3

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.

Beena
  • 2,334
  • 24
  • 50
  • Marshmallow requires permission from the user, its a new feature introduced starting from Android M. Implement permission manager to grant access and do it. – Brendon Jan 23 '16 at 05:19
  • Yes. I am aware about it but it is if my target sdk is 23 right? But my target sdk is 22. – Beena Jan 23 '16 at 05:20
  • Whatever, but u are debugging directly to Marshmallow right? so if marshmallow sdk is utilized the code also need to alter to its supportive to work – Brendon Jan 23 '16 at 05:24
  • But if it is the reason then i should check for another permission too like camera,Gps and other. But those functionalities are working fine. So i was confused. Let me try by checking permissions once. – Beena Jan 23 '16 at 05:28
  • I don't think it's permission issue in 6.0 . Have checked both permission for calendar and it is granted . – Beena Jan 23 '16 at 05:44
  • refer this https://productforums.google.com/forum/#!topic/nexus/0UZ8JR7bL1U – Radhey Jan 23 '16 at 06:32
  • did you solved the problem iam facing the same issue – prasanthMurugan Sep 23 '16 at 09:29

3 Answers3

2

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.

DHIRAJ KATEKAR
  • 208
  • 1
  • 8
  • How can I solve it programmatically? I'm having the same issue of @Beena but on a user device, and so I cannot force him/her to configure an account. – LucioB Aug 02 '17 at 09:21
0

Try below link:

How to add multiple event to android marshmallow Calendar?

Community
  • 1
  • 1
Lucky Rana
  • 1,030
  • 1
  • 12
  • 20
0

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'
jps
  • 20,041
  • 15
  • 75
  • 79
Avinash Ajay Pandey
  • 1,497
  • 12
  • 19