0

I am using a code to add an event to the device's calendar. The code works fine in all the Android versions except Android M.The event when being added on Android M shows as a birthday instead of an event. Please help on how can this issue be fixed.

The code being used is as follows

     // Add event to calendar
        try {
            ContentResolver cr = context.getContentResolver();
            ContentValues values = new ContentValues();
            values.put(CalendarContract.Events.DTSTART, startDateInMilliSeconds);
            values.put(CalendarContract.Events.DTEND, endDateInMilliSeconds);
            values.put(CalendarContract.Events.TITLE, title);
            values.put(CalendarContract.Events.DESCRIPTION, description);
            if (location != null && (!TextUtils.isEmpty(location))) {
                values.put(CalendarContract.Events.EVENT_LOCATION, location);
            }
            values.put(CalendarContract.Events.CALENDAR_ID, 1);
            values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
                    .getTimeZone().getID());             
            Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
            // Save the eventId into the Task object for possible future delete.
            long eventId = Long.parseLong(uri.getLastPathSegment());            
            Uri openCalendarEvent = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
            Intent calIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDateInMilliSeconds);
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDateInMilliSeconds);
            calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(calIntent);              

        } catch (Exception e) {
            e.printStackTrace();

        }
Himmat Gill
  • 341
  • 3
  • 4

1 Answers1

0

try below method @ Himmat

public void addReminder() {
        Intent calIntent = new Intent(Intent.ACTION_EDIT);
        calIntent.setData(CalendarContract.Events.CONTENT_URI);
        calIntent.setType("vnd.android.cursor.item/event");
        calIntent.putExtra(CalendarContract.Events.TITLE, e.getName());
        calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, e.getLocation());
        calIntent.putExtra(CalendarContract.Events.DESCRIPTION, e.getDescription());
        Calendar calStart=Calendar.getInstance();
        if(Validator.isNotNull(e.getStartDate())) {
            calStart.setTime(e.getStartDate());
        }
        calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                calStart.getTimeInMillis());
        Calendar calEnd=Calendar.getInstance();
        if(Validator.isNotNull(e.getEndDate())) {
            calEnd.setTime(e.getEndDate());
        }
        calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                calEnd.getTimeInMillis());
        calIntent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI,
                e.getRegistrationLink());
        startActivity(calIntent);
    }
Hardik Mehta
  • 867
  • 1
  • 12
  • 20
  • Hi, this method will open the intent for adding an event. The question of for adding an event progammatically through the code itself without having to open an intent. – Himmat Gill Aug 19 '16 at 05:25
  • @ Himmat see http://stackoverflow.com/questions/5976098/how-to-set-a-reminder-in-android sanjeev kumar's answer – Hardik Mehta Aug 19 '16 at 05:32
  • I have already tried all of them brother. The issue woth the code that I have written above is that for Android M devices the calendarID is not one. As a result no event is created and instead a birthday notification is created in place of it. So the issue in my code is particularly for Android M devices – Himmat Gill Aug 19 '16 at 05:35
  • @ Himmat have you added any account in calender or Map? – Hardik Mehta Aug 19 '16 at 05:45
  • No i did not. Till Android M the calendarID by default used to be 1. So this is something that is OS specific and I am not able to find a proper solution for the same. The only things I think of here is eother calling an intent or creating a local calendar. However, I am not satisfied with these solutions and wanted to know if someone has a better approach to it. – Himmat Gill Aug 20 '16 at 08:15