1

I've build an app which allows to synchronize the app specific data with the google calendar.

To add my events to the calendar I've got the following method, which works fine.

private static void addCalendarEvents(Context context, Cursor c) {
        if (c.moveToFirst()) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {

                while (!c.isAfterLast() && !c.isBeforeFirst()) {
                    ContentResolver cr = context.getContentResolver();
                    ContentValues values = new ContentValues();

                    values.put(CalendarContract.Events.DTSTART, start);
                    values.put(CalendarContract.Events.DTEND, end);
                    values.put(CalendarContract.Events.TITLE, title);
                    values.put(CalendarContract.Events.DESCRIPTION, description);
                    values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
                    values.put(CalendarContract.Events.CALENDAR_ID, 1);
                    values.put(CalendarContract.Events.HAS_ALARM, false);

                    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

                    c.moveToNext();
                }
            }
        }
    }

The added events are visible in the google calendar app. My problem is, that I also want to be able to delete (and update) MY EVENTS from the calendar again. How can I achieve that? I only found solutions to remove all events from the calendar.

Do I have to create an own calendar? How can I automatically let this calendar be synced with the google calendar? Or do I have to save all event ids I added?

Endzeit
  • 4,810
  • 5
  • 29
  • 52
  • You should be able to remove an event just by passing the appropriate `calendarId` and `eventId` as indicated in the Calendar API [Events.delete](https://developers.google.com/google-apps/calendar/v3/reference/events/delete) documentation. This should be easier for Android since (from what I know) the API client already handles API calls through their classes (which you just need to call). – adjuremods Jan 20 '16 at 04:25

1 Answers1

0

I decided to save the calendarEventIds as a additional column in my database as follows which works as a charm:

private static void addCalendarEvents(Context context, Cursor c) {
    if (c.moveToFirst()) {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {

            while (!c.isAfterLast() && !c.isBeforeFirst()) {
                ContentResolver cr = context.getContentResolver();
                ContentValues values = new ContentValues();

                values.put(CalendarContract.Events.DTSTART, start);
                values.put(CalendarContract.Events.DTEND, end);
                values.put(CalendarContract.Events.TITLE, title);
                values.put(CalendarContract.Events.DESCRIPTION, description);
                values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
                values.put(CalendarContract.Events.CALENDAR_ID, 1);
                values.put(CalendarContract.Events.HAS_ALARM, false);

                long eventID;
                // The row has an event id, so the event should be updated not created
                if ((eventID = c.getLong(c.getColumnIndex(LessonDatabaseManagement.KEY_CALENDAR_EVENT_ID))) != -1) {
                    Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
                    cr.update(eventUri, values, null, null);
                } else {
                // The row hasn't an event id, so the event should be created
                    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
                    eventID = Long.parseLong(uri.getLastPathSegment());
                    new LessonDatabaseManagement(this)
                            .updateCalendarEventId(c.getInt(c.getColumnIndex(LessonDatabaseManagement.KEY_ID)), eventID);
                }

                c.moveToNext();
            }
        }
    }
}

To delete:

private void removeCalendarEvents() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
        Cursor c = new LessonDatabaseManagement(this).getAllCalendarEventIds();

        if (c.moveToFirst()) {

            while (!c.isAfterLast() && !c.isBeforeFirst()) {
                long eventID;
                if ((eventID = c.getLong(c.getColumnIndex(LessonDatabaseManagement.KEY_CALENDAR_EVENT_ID))) != -1) {
                    Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
                    this.getContentResolver().delete(eventUri, null, null);
                }

                c.moveToNext();
            }
        }

        new LessonDatabaseManagement(this).removeAllCalendarEventIds();
    }
}
Endzeit
  • 4,810
  • 5
  • 29
  • 52