且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除日历特定事件

更新时间:2023-12-03 14:07:46

从日历中读取数据后刚刚尝试了这一点。

添加单发生的事件到日历

要添加一个条目到一个特定的日历,我们需要配置日历项使用ContentValues​​插入如下:

  ContentValues​​事件=新ContentValues​​();
 

每个事件需要被捆绑到特定的日历,所以你会想设置的第一件事是日历的标识插入此事件为:

  event.put(CALENDAR_ID的calid);
 

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

  event.put(标题,活动名称);
event.put(说明,事件说明);
event.put(eventLocation,活动地点);
 

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

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

 长的startTime = START_TIME_MS;
长endTime的= END_TIME_MS;
event.put(DTSTART的startTime);
event.put(DTEND,endTime的);
 

如果我们要添加生日或节日,我们会设置的入口是一个全天事件:

  event.put(allDay,1); // 0为假,1为真
 

这个信息足以让大多数条目。但是,也有一些其他有用的日历条目的属性。

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

  event.put(eventStatus,1);
 

您可以控制​​谁可以通过设置它的可见性默认看到此事件(0),保密(1),私营(2),或公共(3):

  event.put(知名度,0);
 

您可以控制​​是否事件消耗的时间(可以有时间冲突)在日历上通过其透明度设置为不透明(0)或透明(1)。

  event.put(透明度,0);
 

您可以控制​​是否某个事件触发提醒报警如下:

event.put(hasAlarm,1); // 0为假,1表示真

在日历事件被正确配置,我们就可以使用 ContentResolver的插入新的日历进入相应的URI的日历事件:

 乌里eventsUri = Uri.parse(内容://日历/事件);
  乌里URL = getContentResolver()插入(eventsUri,事件)。
 

要插入()方法接触的日历内容提供商,并试图插入进入相应的用户日历的电话。如果您导航到日历应用程序并启动它,你应该看到在适当的日历中的日历项。由于日历同步,你还会看到日历项在线,如果您使用的是网络上的谷歌日历。

删除事件

 私人诠释DeleteCalendarEntry(INT ENTRYID){
        INT iNumRowsDeleted = 0;

        乌里eventsUri = Uri.parse(getCalendarUriBase()+事件);
        乌里eventUri = ContentUris.withAppendedId(eventsUri,ENTRYID);
        。iNumRowsDeleted = getContentResolver()删除(eventUri,NULL,NULL);

        Log.i(DEBUG_TAG,已删除+ iNumRowsDeleted +日历项。);

        返回iNumRowsDeleted;
    }
 

另外通过这个链接删除

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

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