2012-10-22 63 views
0

我想删除所有日历条目。我正在使用此查询获取日历条目删除日历条目

Uri uri = Uri.parse("content://com.android.calendar/events"); 
cursor = context.getContentResolver().query(uri, null, null,null, null); 

但是,游标每次都会返回空值。我也用 Uri uri = Uri.parse("content://calendar/events");检查它,但结果是一样的。

请帮忙。

在此先感谢。

+0

什么是有针对性的API级别? (在API级别14/Android 4.0之前,日历API没有标准化)。 – Stefan

+0

目标api是api等级8及以上。 –

回答

0

//试试这个REQ​​

public void deleteAllCalendar(){ 
     Log.i(TAG, "In deleteAllCalendar()"); 
     String strUriEvents = "content://calendar/events"; 
     Uri uri_calendar = Uri.parse(strUriEvents); 
     String str_column_name = "_id"; 
     String[] projection = {str_column_name}; 
     int columnIndex = 0; 
     String str_id = ""; 
     Vector<String> vector_id = new Vector<String>(); 
     int delRow = 0; 
     String where = ""; 
     try { 
      Cursor cursor = cr.query(uri_calendar, projection, null, null, null); 
      if(cursor.moveToFirst()){ 
       do{ 
        columnIndex = cursor.getColumnIndex(str_column_name); 
        str_id = cursor.getString(columnIndex); 
        vector_id.add(str_id); 
       }while(cursor.moveToNext()); 
      } 
      cursor.close(); 
      for(int i=0; i<vector_id.size(); i++){ 
       str_id = vector_id.get(i); 
       where = str_column_name+"="+str_id; 
       delRow = cr.delete(uri_calendar, where, null); 
       Log.i(TAG, "deleteAllCalendar(),delRow:"+delRow); 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      Log.e(TAG, "deleteAllCalendar(),Exception"); 
      e.printStackTrace(); 
     } 
     Log.i(TAG, "Out deleteAllCalendar()"); 
    } 
+0

它也在游标中给null。 –

+0

@ unflagged.destination did U add READ_CALENDAR&\t WRITE_CALENDAR \t允许应用程序写入(但不读取)用户的日历数据。您的manifest.xml中的权限是 –

+0

是的,但游标中的结果相同。 –

0

的问题是URI的一部分日历权威。它在API级别14之前没有标准化。如果您的目标级别为8级或更高,Google代码建议com.android.calendar,但手机制造商可能使用其他机构。在API 8之前,授权仅为calendar(如前面的答案中所用)。

还记得授予写入用户日历的权​​限。

当您删除事件时,请记住删除其扩展属性,提醒和警报。以下是其路径:

private static final String calendarPath = "calendars"; 
private static final String eventsPath = "events"; 
private static final String remindersPath = "reminders"; 
private static final String calAlertsPath = "calendar_alerts"; 
private static final String eventsExtPropPath = "extendedproperties"; 

随着API级别14,这是标准化的,你可以从CalendarContract得到URI:

CalendarContract.Calendars.CONTENT_URI; 
    CalendarContract.Events.CONTENT_URI; 
    CalendarContract.Reminders.CONTENT_URI; 
    CalendarContract.CalendarAlerts.CONTENT_URI; 
    CalendarContract.ExtendedProperties.CONTENT_URI; 
+0

是的,我检查了两个URI,但总是光标给空 –

+0

你检查了上述权限(日历读取现在)? – Stefan

+0

是的,但游标返回null。 –