Delete calendar entries

前端 未结 2 1428
既然无缘
既然无缘 2020-12-10 07:27

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

Source code:

public void DeleteEvent(View view){

          


        
相关标签:
2条回答
  • 2020-12-10 08:08

    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);
            }
        }
    
    0 讨论(0)
  • 2020-12-10 08:18

    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);
    
    0 讨论(0)
提交回复
热议问题