Get Events From All Accounts Of The Device And Only Events For Today
Solution 1:
What you need to do is query the calendars, and for each of them, query the events instances and just count them. Something like that
// Projection array. Creating indices for this array instead of doing// dynamic lookups improves performance.privatestaticfinal String[] CALENDAR_PROJECTION = newString[] { Calendars.ACCOUNT_NAME };
privatestaticfinal String[] INSTANCE_PROJECTION = newString[] { Instances._ID };
// The indices for the projection array above.privatestaticfinalintPROJECTION_ACCOUNT_NAME_INDEX=0;
// this is the method that returns the count of all events for all the calendarspublicstaticintgetAllEventsCount(final Context context)
{
inteventsCount=0;
// queries the calendarsfinalCursorcalendarCursor= context.getContentResolver().query(Calendars.CONTENT_URI, CALENDAR_PROJECTION, null, null, null);
while (calendarCursor.moveToNext())
{
// gets the calendar name - you need that to query the instances for each calendarfinalStringaccountName= calendarCursor.getString(PROJECTION_ACCOUNT_NAME_INDEX);
eventsCount = eventsCount + getCalendarEventsCount(context, accountName);
}
calendarCursor.close();
return eventsCount;
}
// this method gets a count of the events in a given calendarprivatestaticintgetCalendarEventsCount(final Context context, final String accountName)
{
finalCalendarbeginTime= Calendar.getInstance();
finalCalendarendTime= Calendar.getInstance();
// get events from the start of the day until the same evening.
beginTime.set(beginTime.get(Calendar.YEAR), beginTime.get(Calendar.MONTH), beginTime.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
endTime.set(beginTime.get(Calendar.YEAR), beginTime.get(Calendar.MONTH), beginTime.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
finallongstartMillis= beginTime.getTimeInMillis();
finallongendMillis= endTime.getTimeInMillis();
// Construct the query with the desired date range.final Uri.Builderbuilder= Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, startMillis);
ContentUris.appendId(builder, endMillis);
finalStringselection="(" + Calendars.ACCOUNT_NAME + " = ?)";
final String[] selectionArgs = newString[] { accountName };
finalCursorcursor= context.getContentResolver().query(builder.build(), INSTANCE_PROJECTION, selection, selectionArgs, Instances.BEGIN + " ASC");
intcount= cursor.getCount();
cursor.close();
return count;
}
This has been tested and it works. Hope it helps.
Solution 2:
Use Instances.CONTENT_BY_DAY_URI to get events of specific day. You can find an example of such query here - https://github.com/CyanogenMod/android_packages_apps_Calendar/blob/cm-12.0/src/com/android/calendar/Event.java#L307
Solution 3:
Did you look at Reading all of today's events using CalendarContract - Android 4.0+ ? But you would still need to query each calendar separately. You code is querying all events and post-processing whereas you should filter the start and end times in the query.
A content resolver is designed to return content, and is not a general purpose SQL that can return only a count. To make it more efficient, you would want to query just the id column and get the cursor row count.
Cursor.getCount()
Post a Comment for "Get Events From All Accounts Of The Device And Only Events For Today"