简体   繁体   English

如何通过ID添加日历事件,并通过ID删除日历事件? (Android 4.0)

[英]How to add calendar events by their id and delete them by their id? (Android 4.0)

With this code my team mate was adding calendar events. 通过此代码,我的队友可以添加日历事件。 This works without problems. 这没有问题。 The calendar events are shown in the google calendar. 日历事件显示在Google日历中。

    TimeZone timeZone = TimeZone.getDefault();
        ContentResolver cr = getContentResolver();
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
        values.put(CalendarContract.Events.DTSTART, startInMillis);
        values.put(CalendarContract.Events.DTEND, endInMillis);
        values.put(CalendarContract.Events.TITLE, title);
        values.put(CalendarContract.Events.EVENT_LOCATION, "Raum: " + location);
        values.put(CalendarContract.Events.DESCRIPTION,
                "Dauer der Veranstaltung: " + duration + " min" + "---|---Dozent: "
                        + organizer.trim() + "---|---Course added by UNIDATE---|");
        values.put(CalendarContract.Events.CALENDAR_ID, 1);
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
        ContentUris.withAppendedId(uri, id);

But now I want to delete these events by their id. 但是现在我想通过事件ID删除这些事件。 I've used this code (among others): 我使用了以下代码(以及其他代码):

long selectedEventId = 1;
String[] selArgs = new String[]{Long.toString(selectedEventId)};
int deleted = getContentResolver().delete(CalendarContract.Events.CONTENT_URI, Events._ID + " =? ", selArgs);

This doesn't work. 这行不通。 I also tried to add 我也尝试添加

values.put(CalendarContract.Events._ID, id);

A simple solution will be to first clear the calendar events, and then add the relevant events. 一个简单的解决方案是首先清除日历事件,然后添加相关事件。

Clearing the calendar events can be done by calling this: 清除日历事件可以通过调用以下命令来完成:

context.getContentResolver().delete(CalendarContract.Events.CONTENT_URI, CalendarContract.Events.CALENDAR_ID + " = ?", new String[]{String.valueOf(calendarId)});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM