开发者

Android,I need the code that delete all the event of my calendar in my device

开发者 https://www.devze.com 2023-03-13 10:02 出处:网络
I would like to delete all my event in the calendar,I Have a trying this code: The actual function that should delete:

I would like to delete all my event in the calendar,I Have a trying this code:

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

And that is the code that calling the delete function:

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

I try this code on my Htc Desire but no success i will appreciat Help

the log error:

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.IllegalArgu开发者_运维技巧mentException: 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)

Thank in advence!!!


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

delete a calender event

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号