How to delete a specific event in the calendar

What I want to do is delete only the content that will be saved by me on the calendar, and not all the content that I already have on the calendar. I use the following code for this ... but it will remove all the content from the calendar .. can anyone tell me how this 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 I want to delete only this entry that I put ...

delete 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(); } 
+4
source share
1 answer

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

Adding a Single-Occurrence Event to the Calendar

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

 ContentValues event = new ContentValues(); 

Each event must be associated with a specific Calendar, so the first thing you want to set is the Calendar identifier that inserts this event into:

 event.put("calendar_id", calId); 

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

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

There are several different options for setting the time and date of an event.

We can set the information about the beginning and end of the event as follows:

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

If we add a birthday or a holiday, we would establish that the record is an all-day event:

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

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

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

 event.put("eventStatus", 1); 

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

 event.put("visibility", 0); 

You can control whether the event will consume time (may have schedule conflicts) in the calendar by setting its transparency to opaque (0) or transparent (1).

 event.put("transparency", 0); 

You can control whether the event triggers a reminder:

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

Once the calendar event is configured correctly, we are ready to use ContentResolver to insert a new calendar entry into the corresponding Uri events for the calendar:

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

A call to the insert () method communicates with the calendar content provider and tries to insert it into the corresponding user calendar. If you go to the Calendar application and start it, you should see your calendar entry in the corresponding calendar. Since the calendar is syncing, you will also see a calendar entry on the Internet if you use Google Calendar on the Internet.

Delete 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 follow this link to delete

+4
source

Source: https://habr.com/ru/post/978465/


All Articles