【问题标题】:Get and filter events from Google Calendar从 Google 日历中获取和过滤事件
【发布时间】:2016-08-02 07:18:14
【问题描述】:

在我的 android 应用中,我有代码来编写、更新和删除日历中的事件。

现在我想从日历中获取一些事件,但我不知道事件 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 个事件,尽管我知道它们的描述中有带有字符串的事件。

【问题讨论】:

    标签: android google-calendar-api


    【解决方案1】:

    Calendar API 具有 Freebusy query,您可以在其中使用 timeMintimeMax 在日期之间进行过滤。 检查此SO thread 以获取实际代码示例。

    如果您想根据自己的偏好(标题等)进行查询,则必须进行自己的自定义实现。您可以先尝试获取所有事件,然后对响应进行自定义查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 2017-09-18
      • 1970-01-01
      相关资源
      最近更新 更多