如何删除日历中的特定事件(How to delete specific event in calendar)

编程入门 行业动态 更新时间:2024-10-26 12:22:30
如何删除日历中的特定事件(How to delete specific event in calendar)

我想要做的是只删除我在日历中保存的内容而不是我已经在日历中显示的所有内容..因为我使用以下代码..但它将删除日历的所有内容..谁也可以告诉我如何防止这种情况..

Uri CALENDAR_URI = Uri.parse("content://calendar/events"); ContentResolver cr = getContentResolver(); cr.delete(CALENDAR_URI, null, null); // Delete all ContentValues values = new ContentValues(); values.put("calendar_id", 1); values.put("title", this.title); values.put("allDay", this.allDay); values.put("dtstart", this.dtstart.toMillis(false)); values.put("dtend", this.dtend.toMillis(false)); values.put("description", this.description); values.put("eventLocation", this.eventLocation); values.put("visibility", this.visibility); values.put("hasAlarm", this.hasAlarm); cr.insert(CALENDAR_URI, values);

所以我想要的只是删除我提出的条目...

删除活动

Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events"); ContentResolver cr = c.getContentResolver(); deleteEvent(cr, EVENTS_URI, 1); private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) { Cursor cursor; 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(); }

What i want to do is to delete only the content that are save by me in calendar instead of all the content which i already present in calendar..for that i use the following code..but it will delete all the content of the calendar..so can anyone tell me how that can be prevented..

Uri CALENDAR_URI = Uri.parse("content://calendar/events"); ContentResolver cr = getContentResolver(); cr.delete(CALENDAR_URI, null, null); // Delete all ContentValues values = new ContentValues(); values.put("calendar_id", 1); values.put("title", this.title); values.put("allDay", this.allDay); values.put("dtstart", this.dtstart.toMillis(false)); values.put("dtend", this.dtend.toMillis(false)); values.put("description", this.description); values.put("eventLocation", this.eventLocation); values.put("visibility", this.visibility); values.put("hasAlarm", this.hasAlarm); cr.insert(CALENDAR_URI, values);

so what i want is to delete only that entry that are put by me...

deleting the event

Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events"); ContentResolver cr = c.getContentResolver(); deleteEvent(cr, EVENTS_URI, 1); private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) { Cursor cursor; 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(); }

最满意答案

从日历中读取数据后,试试这个..

将单个事件事件添加到日历

要向特定日历添加条目,我们需要使用ContentValues配置要插入的日历条目,如下所示:

ContentValues event = new ContentValues();

每个事件都需要绑定到特定的日历,因此您要设置的第一件事是将此事件插入到的日历的标识符:

event.put("calendar_id", calId);

然后,我们设置了一些有关事件的基本信息,包括字符串字段,例如事件标题,描述和位置。

event.put("title", "Event Title"); event.put("description", "Event Desc"); event.put("eventLocation", "Event Location");

有许多不同的选项可用于配置事件的时间和日期。

我们可以按如下方式设置事件开始和结束信息:

long startTime = START_TIME_MS; long endTime = END_TIME_MS; event.put("dtstart", startTime); event.put("dtend", endTime);

如果我们要添加生日或假日,我们会将条目设置为全天活动:

event.put("allDay", 1); // 0 for false, 1 for true

此信息足以满足大多数条目的需要。 但是,还有许多其他有用的日历条目属性。

例如,您可以将事件状态设置为暂定(0),确认(1)或取消(2):

event.put("eventStatus", 1);

您可以通过将其可见性设置为默认值(0),机密(1),私有(2)或公共(3)来控制谁可以查看此事件:

event.put("visibility", 0);

您可以通过将其透明度设置为opaque(0)或transparent(1)来控制事件是否消耗时间(可能有计划冲突)。

event.put("transparency", 0);

您可以控制事件是否触发提醒警报,如下所示:

event.put(“hasAlarm”,1); // 0表示false,1表示true

正确配置日历事件后,我们就可以使用ContentResolver将新日历条目插入到日历事件的相应Uri中:

Uri eventsUri = Uri.parse("content://calendar/events"); Uri url = getContentResolver().insert(eventsUri, event);

对insert()方法的调用与Calendar内容提供程序联系,并尝试将该条目插入相应的用户Calendar。 如果您导航到日历应用程序并启动它,您应该在相应的日历中看到您的日历条目。 自“日历”同步以来,如果您在网络上使用Google日历,则还会在线查看“日历”条目。

删除活动

private int DeleteCalendarEntry(int entryID) { int iNumRowsDeleted = 0; Uri eventsUri = Uri.parse(getCalendarUriBase()+"events"); Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID); iNumRowsDeleted = getContentResolver().delete(eventUri, null, null); Log.i(DEBUG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry."); return iNumRowsDeleted; }

另请浏览此链接以进行删除

After reading the data from the Calendar just try this out..

Adding a Single-Occurrence Event to a Calendar

To add an entry to a specific calendar, we need to configure a calendar entry to insert using the ContentValues as follows:

ContentValues event = new ContentValues();

Each event needs to be tied to a specific Calendar, so the first thing you're going to want to set is the identifier of the Calendar to insert this event into:

event.put("calendar_id", calId);

We then set some of the basic information about the event, including String fields such as the event title, description and location.

event.put("title", "Event Title"); event.put("description", "Event Desc"); event.put("eventLocation", "Event Location");

There are a number of different options for configuring the time and date of an event.

We can set the event start and end information as follows:

long startTime = START_TIME_MS; long endTime = END_TIME_MS; event.put("dtstart", startTime); event.put("dtend", endTime);

If we are adding a birthday or holiday, we would set the entry to be an all day event:

event.put("allDay", 1); // 0 for false, 1 for true

This information is sufficient for most entries. However, there are a number of other useful calendar entry attributes.

For example, you can set the event status to tentative (0), confirmed (1) or canceled (2):

event.put("eventStatus", 1);

You can control who can see this event by setting its visibility to default (0), confidential (1), private (2), or public (3):

event.put("visibility", 0);

You can control whether an event consumes time (can have schedule conflicts) on the calendar by setting its transparency to opaque (0) or transparent (1).

event.put("transparency", 0);

You can control whether an event triggers a reminder alarm as follows:

event.put("hasAlarm", 1); // 0 for false, 1 for true

Once the calendar event is configured correctly, we're ready to use the ContentResolver to insert the new calendar entry into the appropriate Uri for calendar events:

Uri eventsUri = Uri.parse("content://calendar/events"); Uri url = getContentResolver().insert(eventsUri, event);

The call to the insert() method contacts the Calendar content provider and attempts to insert the entry into the appropriate user Calendar. If you navigate to the Calendar application and launch it, you should see your calendar entry in the appropriate Calendar. Since the Calendar syncs, you will also see the Calendar entry online, if you're using the Google Calendar on the web.

Delete the event

private int DeleteCalendarEntry(int entryID) { int iNumRowsDeleted = 0; Uri eventsUri = Uri.parse(getCalendarUriBase()+"events"); Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID); iNumRowsDeleted = getContentResolver().delete(eventUri, null, null); Log.i(DEBUG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry."); return iNumRowsDeleted; }

Also go through this link for deleting

更多推荐

本文发布于:2023-08-05 05:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日历   事件   delete   calendar   event

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!