How to get calendar events with title including re

2019-05-30 17:42发布

问题:

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?

回答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



回答2:

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