android - 删除日历项

标签 android calendar

我编写了简单的代码来删除 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);
            }
        }

最佳答案

我用它来删除:

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

关于android - 删除日历项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5806174/

相关文章:

相对于复活节及时计算事件

android - 两次访问 OkHttp 响应的正文字符串导致 IllegalStateException : closed

java - Google Drive Android API OAuth 身份验证问题 - onActivityResult 返回 "0"

Android - 轨迹球事件结束了吗?

json - 如何在 swift 中创建特定的数据结构。 Swifty Json 没有用 :(

java - 如何在日历中显示两位数?

java - Android 上通过蓝牙进行数据处理导致数据完整性不佳

android - 当我添加工具时发生了什么 :replace ="android:appComponentFactory"

java - 如何将贾拉利日历与回历日历相匹配

java - Java中的日期运算?