TamarG
TamarG

Reputation: 3570

Get and filter events from Google Calendar

In my android app, I have the code to write, update and delete events from/to calendars.

Now I want to get some events from a calendar but I don't know the event IDs. I want to filter events by title, description, or any other value.

How can I do that?

My code:

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

 }

When I filter by calendar id only - it works and I get all the events in this calendar. When I try to add the "AND description = ? " - I get 0 events although I know there are events with the string in their description.

Upvotes: 1

Views: 2023

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17631

Calendar API has Freebusy query where you conduct a filter between dates using timeMin and timeMax. Check this SO thread for actual code sample.

If you wanted to do a query based on your preferences (title,etc), you'd have to make your own custom implementation. You can try to fetch all the events first then make your own custom query on the response.

Upvotes: 0

Related Questions