Android,我需要删除设备中所有日历事件的代码

时间:2011-06-17 05:06:17

标签: android calendar google-calendar-api

我想删除日历中的所有活动,我尝试了这段代码:

应删除的实际功能:

 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("test"));
        resolver.delete(ContentUris.withAppendedId(eventsUri, eventId), null, null);
    }
    cursor.close();
}

这就是调用delete函数的代码:

        Uri eventsUri;
    int calendarId=1;
    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);

我在我的Htc Desire上尝试这个代码,但没有成功我会欣赏帮助

日志错误:

01-07 18:07:01.305: ERROR/ActivityThread(1958): Failed to find provider info for calendar
01-07 18:07:01.305: ERROR/AndroidRuntime(1958): FATAL EXCEPTION: main
01-07 18:07:01.305: ERROR/AndroidRuntime(1958): java.lang.IllegalArgumentException: Unknown URL content://calendar/events
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at android.content.ContentResolver.delete(ContentResolver.java:671)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at com.DeleteAllContact.Dan.DeleteAllContact.deleteEvent(DeleteAllContact.java:102)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at com.DeleteAllContact.Dan.DeleteAllContact.access$1(DeleteAllContact.java:100)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at com.DeleteAllContact.Dan.DeleteAllContact$2.onClick(DeleteAllContact.java:55)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at android.view.View.performClick(View.java:2408)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at android.view.View$PerformClick.run(View.java:8817)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at android.os.Handler.handleCallback(Handler.java:587)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at android.os.Looper.loop(Looper.java:144)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at android.app.ActivityThread.main(ActivityThread.java:4937)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at java.lang.reflect.Method.invoke(Method.java:521)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-07 18:07:01.305: ERROR/AndroidRuntime(1958):     at dalvik.system.NativeStart.main(Native Method)

感谢advence !!!

1 个答案:

答案 0 :(得分:0)

read calendar database

Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),    null, null, null, null);
cursor.moveToFirst();

// fetching calendars id
if(cursor.getcount>0)
{
CId = new int[cursor.getCount()];
int i=0;
while(!cursor.isAfterLast())
{
     CId[i] = cursor.getInt(cursor.getColumnIndex("_id"));
     i++;
     cursor.moveToNext();
}

删除日历活动

for (int i = 0; i < CNames.length; i++)
{
Uri CALENDAR_URI = Uri.parse("content://calendar/events");
Uri uri = ContentUris.withAppendedId(CALENDAR_URI,Cid[i]);
getContentResolver().delete(uri, null, null);
}
}