2016-08-02 19 views
1

在我的Android應用程序中,我有用於編寫,更新和從日曆中刪除事件的代碼。從Google日曆獲取和過濾事件

現在我想從日曆中獲取一些事件,但我不知道事件ID。我想通過標題,描述或任何其他值過濾事件。

我該怎麼做?

我的代碼:

public static void getEvents(Activity mContext) { 

     Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon(); 
     long now = new Date().getTime(); 
     ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000); 
     ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000); 

     String selection = "((calendar_id = ?) AND (description LIKE ?))"; 
     String[] selectionArgs = new String[] {""+id, "'%abc%'"}; 

     Cursor eventCursor = contentResolver.query(builder.build(), 
       new String[] { "title", "begin", "end", "allDay", "description"}, 
       selection, selectionArgs, 
       "startDay ASC, startMinute ASC"); 

     while (eventCursor.moveToNext()) { 
      String eventTitle = eventCursor.getString(0); 
      String description = eventCursor.getString(4); 
     } 

} 

當我按日曆ID 只有過濾器 - 它的作品,我得到這個日曆中的所有事件。當我嘗試添加「AND description =?」 - 我得到0個事件,雖然我知道在他們的描述中有字符串的事件。

回答

0

日曆API有Freebusy query,您可以使用timeMintimeMax在日期之間進行過濾。 檢查此SO thread爲實際的代碼示例。

如果你想根據你的喜好(標題等)做一個查詢,你必須做出自己的自定義實現。您可以嘗試首先獲取所有事件,然後對響應進行自定義查詢。

相關問題