Delete only one instance of a recurring event from my Android calendar

I was looking at various posts on how to remove only one instance of a recurring event from my Android 2.3 Phone calendar, but I could not find the perfect answer. So far, the best I've come across is:

Uri eventsUri = Uri.parse("content://com.android.calendar/events"); Uri deleteEventUri = Uri.withAppendedPath(eventsUri, String.valueOf(id2)); DeleteEventActivity.context.getContentResolver().delete(deleteEventUri, null, null); 

Where id2 is the identifier of the event I want to delete. The problem is that I want to remove only one instance of a repeating event, but this code removes all occurrences. Is there a way to remove only one instance? Thanks.

+5
source share
3 answers

If anyone else is struggling with this, I finally hacked it!

It turns out that you are not deleting or modifying any entries that you are actually doing is to insert a new event called an exception. It took me several months to find this information anywhere, and even finding it, a few more months to work out what needed to be included in the exception in order to make it work, that’s how I did it.

First, request the actual instance of the event that you want to cancel in one case. You need to query the CalendarContract.Instances table to get the values ​​of CalendarContract.Instances.TITLE, CalendarContract.Instances.BEGIN and CalendarContract.Instances.EVENT_ID. The way I do this in my code does not really fit into the context of this answer, so I hope you can decide how to do it yourself. Save these values ​​as:

final String title = eventCursor.getString(0); final long beginl=eventCursor.getLong(1); final int id = eventCursor.getInt(2);

Then we need to configure the new event as follows:

  final Context context = getApplicationContext(); final ContentResolver contentResolver = context.getContentResolver(); final Uri.Builder builder = Uri.parse( "content://com.android.calendar/events").buildUpon(); final Cursor eventCursor = contentResolver.query(builder.build(), new String[] {Events.CALENDAR_TIME_ZONE,Events.ALL_DAY,Events.CALENDAR_ID,Events._SYNC_ID, Events.OWNER_ACCOUNT }, "_id=" + id, null, null); while (eventCursor.moveToNext()) { final String timezone=eventCursor.getString(0); final String allday=eventCursor.getString(1); final long calID=eventCursor.getLong(2); final String mSyncId=eventCursor.getString(3); final String account=eventCursor.getString(4); ContentValues values = new ContentValues(); values.put(Events.TITLE, title); values.put(Events.EVENT_TIMEZONE, timezone); values.put(Events.ORIGINAL_SYNC_ID, mSyncId);//not 100% sure about this,may also need a date? values.put(Events.HAS_ALARM, "0"); values.put(Events.HAS_ATTENDEE_DATA,"0"); values.put(Events.ALL_DAY, allday); values.put(Events.DTSTART, beginl+3600000); values.put(Events.ORIGINAL_INSTANCE_TIME, beginl+3600000); values.put(Events.STATUS, Events.STATUS_CANCELED); Uri uri = Uri.withAppendedPath(Events.CONTENT_EXCEPTION_URI, String.valueOf(id)); uri = asSyncAdapter(uri, account, CTS_TEST_TYPE); Uri resultUri = context.getContentResolver().insert(uri, values); try { int eventID = Integer.parseInt(resultUri.getLastPathSegment()); int debug=eventID; } catch (Exception e) { int debug=0; } } static Uri asSyncAdapter(Uri uri, String account, String accountType) { return uri.buildUpon() .appendQueryParameter (android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true") .appendQueryParameter(Calendars.ACCOUNT_NAME, account) .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build(); } 

Hope this works, I tried to cut out parts of my code that are not relevant. You will notice that I need to add 3,600,000 to the beginl value (1 hour in milliseconds). I guess this is because we are in BST at the moment, and this code will not work as soon as the clock changes, but I will worry about it at that time!

Finally, I used an application called Content Provider Helper to help me find this. You can use it to query content provider tables. I would try to set up an exception using my code, and then use the Calendar application to delete the instance and compare the two entries.

+6
source

Here are the basics

+6
source

You will need to update the EXDATE column of the source event.

+1
source

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


All Articles