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

前端 未结 5 938
日久生厌
日久生厌 2020-12-16 01:04

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

相关标签:
5条回答
  • 2020-12-16 01:34

    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,

    0 讨论(0)
  • 2020-12-16 01:34

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

    0 讨论(0)
  • 2020-12-16 01:36

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

    0 讨论(0)
  • 2020-12-16 01:44

    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);
    
    0 讨论(0)
  • 2020-12-16 01:53

    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.

    0 讨论(0)
提交回复
热议问题