I need to get the number of events on the calendars for today. Only the number of events of all calendars, do not need anything more. I found several threads about it, but I can not get the number of events for today.
using this code, the variable "events" gives me the number of all events in the first calendar.
int events=0;
public void read() {
Context context = getApplicationContext();
ContentResolver contentResolver = context.getContentResolver();
long ntime = System.currentTimeMillis();
Cursor cursr = contentResolver.query(Uri.parse("content://com.android.calendar/events"), new String[]{ "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, "calendar_id=1", null, null);
cursr.moveToFirst();
String[] CalNames = new String[cursr.getCount()];
int[] CalIds = new int[cursr.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursr.getInt(0);
CalNames[i] = "Event"+cursr.getInt(0)+": \nTitle: "+ cursr.getString(1)+"\nDescription: "+cursr.getString(2)+"\nStart Date: "+cursr.getLong(3)+"\nEnd Date : "+cursr.getLong(4)+"\nLocation : "+cursr.getString(5);
long StartTime = cursr.getLong(3);
long EndTime = cursr.getLong(4);
if ((StartTime<ntime)&&(ntime<EndTime)) {
System.out.println("In the middle of something");
break;
}
cursr.moveToNext();
events++;
}
cursr.close();
Log.i("control","vueltas:"+events);
events=0;
}
I need to modify the code to read events from all calendars of the device, and only events for today. several days looking for how to do it, but the solutions I've found (including this page) do not know how to apply my code... There is a very similar to my question, but has no answer https://stackoverflow.com/questions/18301870/get-todays-calendar-events
I appreciate your help.
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
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
This has been tested and it works. Hope it helps.
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.