Reading all of today's events using CalendarContract - Android 4.0+

断了今生、忘了曾经 提交于 2019-11-29 01:52:34

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.

SebastienS

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,

you should be able to add the CalendarContract.Events.ALL_DAY to your selection condition to filter for all ALL_DAY events.

I know this is a bit late, but I had a very similar question and had trouble finding the answer I was looking for. The forced-UTC time zone for all-day events made things tricky. Here's my solution:

    // "allDayStart" is an all-day event today, encoded in the default time zone
    Time allDayStart = new Time();
    allDayStart.timezone=TimeZone.getDefault().toString();
    allDayStart.set(dayStart.monthDay, dayStart.month, dayStart.year);

    // 2 time selections for the query: 
        // 1) Between day-start and day-end (not all-day); or
        // 2) Equals today at 0:00:00 (all-day) in the default timezone
    String calSelection = 
        "((" + Calendars.ACCOUNT_NAME + " = ?) " +
            "AND (" + Calendars.OWNER_ACCOUNT + "= ?) " +
            "AND (" +
                "((" + Events.DTSTART + ">= ?) " +
                "AND (" + Events.DTSTART + "<= ?) " +
                "AND (" + Events.ALL_DAY + "= ?) " +
                ") " +
            "OR ((" + Events.DTSTART + "= ?) " +
                "AND (" + Events.ALL_DAY + "= ?)" +
                ")" +
            ")" +
        ")"; 

    String[] calSelectionArgs = new String[] {
        accountName, ownerName, 
        dayStartInMillis.toString(), dayEndInMillis.toString(), "0", // during today, not all day
        allDayStartInMillis.toString(), "1" // Started today at default start-time for all-day events (all-day), default time zone
    }; 

The query could be refined to not need 2 parts, but this was good enough for me.

In case it helps, here's where dayStart and dayEnd came from:

    Time dayStart = new Time();
    dayStart.setToNow();
    dayStart.hour=0;
    dayStart.minute=0;
    dayStart.second=0;

    Time dayEnd = new Time();
    dayEnd.set(dayStart);
    dayEnd.hour=dayStart.hour+23;
    dayEnd.minute=dayStart.minute+59;
    dayEnd.second=dayStart.second+59;

    Long dayStartInMillis = dayStart.toMillis(false);
    Long dayEndInMillis = dayEnd.toMillis(false) + 999;
    Long allDayStartInMillis = allDayStart.toMillis(false);
alcsan

Try to use Instances.CONTENT_BY_DAY_URI. Check this answer for an example - https://stackoverflow.com/a/36622111/1219241

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!