6

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);
            }
        }
1
  • Thanks Man @Husky .. its working for me :) Commented Feb 6, 2016 at 13:39

2 Answers 2

7

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);
3
  • Android can have (and usually has) multiple calendars. CalendarId is integer that represents calendar that you wish to use. Commented Aug 3, 2011 at 15:58
  • but here it will delete all the event that are present in the calendar..but if i want to delete only that event that are put by my app not the event that are already there in my calendar than how that can be done...
    – AndroidDev
    Commented Aug 22, 2011 at 7:07
  • @AndroidDev did you get the solution. please help me, I am stuck with the same problem. Commented Mar 24, 2019 at 10:10
0

use this code its right

        public void DeleteEvent(int your_event_id){

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

        while (cur.moveToNext()){

             Uri eventUri = ContentUris.withAppendedId(eventsUri, your_event_id);
            iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.