I'm trying to use Android's new calendar API to read all of today's calendar events. I'm have trouble finding the right selection on the database query to return all of the events. It seems that all recurring and all day events are left out of the selection. What selection args would permit me to obtain all of today's events from the calendar api?
Here is my current attempt:
Cursor cur = null;
String selection = "((" + CalendarContract.Events.DTSTART
+ " >= ?) AND (" + CalendarContract.Events.DTEND + " <= ?))";
Time t = new Time();
t.setToNow();
String dtStart = Long.toString(t.toMillis(false));
t.set(59, 59, 23, t.monthDay, t.month, t.year);
String dtEnd = Long.toString(t.toMillis(false));
String[] selectionArgs = new String[] { dtStart, dtEnd };
cur = c.getContentResolver().query(CalendarContract.Events.CONTENT_URI,
null, selection, selectionArgs, null);
I am unsure of how to broaden the selection or adding to it to get the recurring events and all day events. Any help would be appreciated.
To get all events today, including recurring events, you need to use the Instances table, i.e.
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI
.buildUpon();
ContentUris.appendId(eventsUriBuilder, timeNow);
ContentUris.appendId(eventsUriBuilder, endOfToday);
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = null;
cursor = mContext.getContentResolver().query(eventsUri, columns, null, null, CalendarContract.Instances.DTSTART + " ASC");
Note that you must append the time constraints to the events uri, you cannot sort any other way.
In order to include all day events as well, just expand the the search to 11:59PM the previous night and 12:00AM tonight.
Your conditions only give you the events that are strictly in today limits. You should check the ones that start before today and end after (multidays event).
For recurring events, I check them manually. I don't found another way.
I use something like:
String selection = "((" + CalendarContract.Events.DTSTART + " <= ?) AND (" + CalendarContract.Events.DTEND + " >= ?)) OR (" + CalendarContract.Events.RRULE + " is not null )";
String[] selectionArgs = new String[] { dtEnd, dtStart};
Regards,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With