我从互联网上搜索了很多,并尝试了很多例子。我可以通过我的应用程序成功地将事件添加到日历中,但我无法以编程方式删除此事件。这是我尝试过的样本,但我无法获得成功的结果。
令牌[1] 是事件 ID。
1)
Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
Uri eventUri = ContentUris.withAppendedId(eventsUri, Long.parseLong(tokens[1]));
getContentResolver().delete(eventUri, null, null);
2)
ContentResolver cr = FlightOperationsCancelTicketFee.this.getContentResolver();
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events");
deleteEvent(cr, EVENTS_URI, 1);
private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId)
{
Cursor cursor;
if (android.os.Build.VERSION.SDK_INT <= 7)
{
cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars_id=" + calendarId, null, null);
}
else
{
cursor = resolver.query(eventsUri, new String[]{ "_id" }, "calendar_id=" + calendarId, null, null);
}
while(cursor.moveToNext())
{
long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
resolver.delete(ContentUris.withAppendedId(eventsUri, eventId), null, null);
}
cursor.close();
}
3)
ContentResolver cr = getContentResolver();
String calUriString = "content://com.android.calendar/events";
Uri cal=Uri.parse(calUriString);
String[] EVENT_PROJECTION=new String[]{"calendar_id","title","dtstart","_id"};
Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
Uri eventUri =ContentUris.withAppendedId(eventsUri, Long.parseLong(tokens[1]));
String reminderUriString = "content://com.android.calendar/reminders";
Uri remUri =Uri.parse(reminderUriString);
cr.delete(remUri, "event_id="+Commons.event_id, null);
cr.delete(eventUri, null, null);
4)
Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, Long.parseLong(tokens[1]));
getContentResolver().delete(eventUri, null, null);
以上都不起作用。我需要帮助 。谢谢.. 编辑:我想我无法发送正确的上下文,有没有办法通过共享偏好来保持上下文?但是,它只保留 String 和 Int 值。还有另一种方法可以做这样的事情吗?