删除日历条目

发布于 2021-01-30 17:33:56

我编写了用于删除android日历中所有条目的简单代码,但并未删除任何内容。

源代码:

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);
            }
        }
关注者
0
被浏览
212
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。
    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();
    }
    

    我这样称呼它:

    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);
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看