Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete calendar entries

I wrote simple code for deleting all entries from android calendar,but it didn't delete nothing.

Source code:

public void DeleteEvent(View view){

            int iNumRowsDeleted = 0;
            Uri eventsUri = Uri.parse("content://com.android.calendar/events");
            Cursor cur = getContentResolver().query(eventsUri, null, null, null, null);

            while (cur.moveToNext()){

                long id = cur.getLong(cur.getColumnIndex("_id"));
                Log.d(TAG, "ID: " + id);
                Uri eventUri = ContentUris.withAppendedId(eventsUri, id);
                iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);
            }
        }
like image 690
Husky Avatar asked Apr 27 '11 14:04

Husky


People also ask

How do I delete events in Apple calendar?

In the Calendar app on your Mac, do one of the following: Select the event, then press the Delete key. If you receive a calendar event from an unknown sender, you can report it as junk and delete it without notifying the sender. Double-click the event, click Report Junk, then click Delete and Report Junk.

Can I delete multiple calendar events at once?

There is a very easy way to bulk delete recurring events in Google Calendar: In Google Calendar, click on any of the recurring events that you want to delete. Click on the trash can symbol in the event popup box.

Can I delete a calendar event?

You can delete events and entire calendars on the Calendar app. You can't delete shared events, but you can decline the invite to get the event to disappear.


1 Answers

I use this for delete:

private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) {
    Cursor cursor;
    if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1 
        cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars._id=" + calendarId, null, null);
    } else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
        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();
}

I call it with something like this:

Uri eventsUri;
int osVersion = android.os.Build.VERSION.SDK_INT;
if (osVersion <= 7) { //up-to Android 2.1 
    eventsUri = Uri.parse("content://calendar/events");
} else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
    eventsUri = Uri.parse("content://com.android.calendar/events");
}
ContentResolver resolver = this.getContentResolver();
deleteEvent(resolver, eventsUri, calendarId);
like image 57
Josip Medved Avatar answered Nov 15 '22 01:11

Josip Medved