从 google 日历中删除自己的活动
Remove own events from google calendar
我构建了一个允许将应用程序特定数据与 google 日历同步的应用程序。
要将我的活动添加到日历,我有以下方法,效果很好。
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 日历应用程序中可见。我的问题是,我还希望能够再次从日历中删除(和更新)我的活动。
我怎样才能做到这一点?我只找到了从日历中删除所有事件的解决方案。
我必须创建自己的日历吗?我怎样才能自动让这个日历与 google 日历同步?或者我是否必须保存我添加的所有事件 ID?
我决定将 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();
}
}
我构建了一个允许将应用程序特定数据与 google 日历同步的应用程序。
要将我的活动添加到日历,我有以下方法,效果很好。
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 日历应用程序中可见。我的问题是,我还希望能够再次从日历中删除(和更新)我的活动。 我怎样才能做到这一点?我只找到了从日历中删除所有事件的解决方案。
我必须创建自己的日历吗?我怎样才能自动让这个日历与 google 日历同步?或者我是否必须保存我添加的所有事件 ID?
我决定将 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();
}
}