从谷歌日历中删除自​​己的事件

时间:2016-01-19 15:03:41

标签: android calendar synchronization google-calendar-api calendarcontract

我已经构建了一个应用程序,可以将应用程序特定数据与谷歌日历同步。

要将我的活动添加到日历中,我有以下方法,该方法正常。

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();
                }
            }
        }
    }

添加的事件在Google日历应用中可见。我的问题是,我还希望能够再次从日历中删除(和更新)MY EVENTS。 我怎样才能做到这一点?我只找到了从日历中删除所有事件的解决方案。

我是否必须创建自己的日历?如何自动让此日历与Google日历同步?或者我是否必须保存我添加的所有事件ID?

1 个答案:

答案 0 :(得分:0)

我决定将calendarEventIds保存为我的数据库中的附加列,如下所示,它可以作为魅力:

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();
            }
        }
    }
}

要删除:

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();
    }
}