I am aware that this is an old post, but I found inspiration for optimizing the solution found in the answer given by Akash Takkar if anyone is in need of a solution in the near future.
The issues
Specically, I found a few issues in the original code:
- The loop for retrieving calendar events broke immaturely
- Hereby, only events from the first calendar was retrieved
- The first event in each calendar was skipped by using eventCursor.moveToFirst(); which thereafter moves directly to the next event in the while loop
- The id of the calendars were not set correctly in the eventCursor
- "Calendars._id=" + 1, should be "Calendars._id=" + id,
- It would be difficult for others to specify their own time range
- The current solution is not object oriented which would hold many advantages
- The readability and documentation is not the best
The solution
I have hereby created a Github Library which returns a list of event objects in a specified time range which can be found at:
https://github.com/david-laundav/CalendarService
The source files can be found under "CalendarService/src/dk/CalendarService".
Use cases
The solution itself contains two different methods for different purposes.
First use case:
CalendarService.readCalendar(class.this)
// where class.this is either your class or the context
This method will return a list of events for +/- 1 day
Second use case:
You can also specify your own time range:
CalendarService.readCalendar(class.this, int days, int hours)
An example might be:
CalendarService.readCalendar(class.this, 2, 5)
answered 2013-06-14T13:59:41.137