I'm trying to log all the events which are happening for 3 days from today in my activity. I'm getting the following errors. Please help. My Code :
public static long getStartOfDayInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static ArrayList<String> readCalendarEvent(Context context) {
Uri.Builder eventsUriBuilder;
eventsUriBuilder = Uri.parse("content://com.android.calendar/events").buildUpon();
ContentUris.appendId(eventsUriBuilder,getStartOfDayInMillis());
ContentUris.appendId(eventsUriBuilder, getStartOfDayInMillis() + 3*(24 * 60 * 60 * 1000));
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = context.getContentResolver()
.query(
eventsUri,
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, CalendarContract.Instances.DTSTART + " ASC");
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
// endDates.add(getDate(Long.parseLong(cursor.getString(4))));
descriptions.add(cursor.getString(2));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return nameOfEvent;
}
I'm calling it to give logs of all the names of the events in the same Java class as..
results= readCalendarEvent(MyCalendarActivity.this);
for(int i=0;i< results.size();i++ )
Log.d("results",results.get(i));
I'm getting the following errors :
Caused by: java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/events/1448821800000/1448908200000
This is because you are using wrong URL. Replace with this:
eventsUriBuilder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
Or:
eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon()