2011-04-27 99 views
5

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

感谢Man @Husky ..它为我工作:) – 2016-02-06 13:39:54

回答

5

我用这个删除:

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

谢谢... =) – Husky 2011-04-27 15:19:29

+0

什么是日历id在这里 – AndroidDev 2011-08-03 11:39:44

+0

Android可以有(通常有)多个日历。 CalendarId是表示您希望使用的日历的整数。 – 2011-08-03 15:58:39

0

使用此代码右

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