Android Calendar Events

These examples are for <= 2.1 version;

first; find out which calendars exist

Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id",  "displayname" }, null, null, null);
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CalNames.length; i++) {
    CalIds[i] = cursor.getInt(0);
    CalNames[i] = cursor.getString(1);
    cursor.moveToNext();
}
cursor.close();

Fetching all events, and particular event is done by specifying range
ContentResolver contentResolver = getContentResolver();

Uri.Builder builder = Uri.parse(getCalendarUriBase() + "/instances/when").buildUpon();
        long now = new Date().getTime();
        ContentUris.appendId(builder, now - DateUtils.MILLIS_PER_DAY*10000);
        ContentUris.appendId(builder, now + DateUtils.MILLIS_PER_DAY * 10000);

and then let’s say you wish to log events ID from calendar with ID = 1

Cursor eventCursor = contentResolver.query(builder.build(),
                new String[] { "event_id"}, "Calendars._id=" + 1,
                null, "startDay ASC, startMinute ASC"); 
        // For a full list of available columns see http://tinyurl.com/yfbg76w
        while (eventCursor.moveToNext()) {
            String uid2 = eventCursor.getString(0);
            Log.v("eventID : ", uid2);

        }

Leave a Comment