2

I need to do a not complicated thing, one could believe. I want to get Event instances from calendars on my phone along with titles of the events. I found several answers on Stack Overflow telling to query Instances table to get recurring events also, which I did, but I can't figure out how to get Event names from this query, because they are in Events table. I found no example of how to do this. I tried to create a custom query by joining Events with Instances, but I don't know if it's even possible to query Calendar tables like this (bypassing API).

Do anybody know how to simply get all Event.TITLE, Instance.DTSTART for given start and end dates?

Episodex
  • 4,479
  • 3
  • 41
  • 58

2 Answers2

1

Ok, I probably need to get some sleep...

Instances table has column CalendarContract.Instances.TITLE which holds the title even though it's not in the Instances table. The query hits some View not the Instances table. I would swear I tried this way and couldn't find TITLE in intellisense for columns.

Full solution here for anybody who may find this useful:

DateTime startDate = DateTime.now().withTime(0, 0, 0, 0);
DateTime endDate = startDate.plusDays(7);

Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI
            .buildUpon();
ContentUris.appendId(eventsUriBuilder, startDate.getMillis());
ContentUris.appendId(eventsUriBuilder, endDate.getMillis());

Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = null;
cursor = getContentResolver().query(
        eventsUri,
        new String[] {CalendarContract.Instances.DTSTART, CalendarContract.Instances.TITLE},
        CalendarContract.Instances.DTSTART + " >= " + startDate.getMillis() + " and " + CalendarContract.Instances.DTSTART + " <= " + endDate.getMillis() + " and " + CalendarContract.Instances.VISIBLE + " = 1",
        null,
        CalendarContract.Instances.DTSTART + " ASC");

This code is using Joda time library

Episodex
  • 4,479
  • 3
  • 41
  • 58
0

This code return all events(recurring events) just add the code in your code " CalendarContract.Instances.VISIBLE + " = 0" +" or "+CalendarContract.Instances.VISIBLE + " = 1" "

it returns all events in a year.

DateTime startDate = DateTime.now().withTime(0, 0, 0, 0);
DateTime endDate = startDate.plusDays(360);

Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI
        .buildUpon();
ContentUris.appendId(eventsUriBuilder,startOfDay.getTimeInMillis());
ContentUris.appendId(eventsUriBuilder, endDate.getMillis());

Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = null;
cursor = context.getContentResolver().query(
        eventsUri,
        new String[]{CalendarContract.Instances.CALENDAR_ID, CalendarContract.Instances.TITLE, CalendarContract.Instances.DESCRIPTION, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END, CalendarContract.Instances.EVENT_LOCATION, CalendarContract.Instances.ORIGINAL_ID, CalendarContract.Instances.ORGANIZER, CalendarContract.Instances.OWNER_ACCOUNT},
        CalendarContract.Instances.BEGIN + " >= " + .getTimeInMillis()+ " and "+CalendarContract.Instances.BEGIN + " <= " + endDate.getMillis()+" and " + CalendarContract.Instances.VISIBLE + " = 0" +" or "+CalendarContract.Instances.VISIBLE + " = 1",
        null,
        CalendarContract.Instances.BEGIN + " ASC");