Alex Rivera | Logout

Getting events from calendar

Asked 2011-05-04T13:10:08.157
28

My issue is, I have to make one demo application in which I wants to read the events of the Google calendar, for that I have manually inserted the events like the title of event, the time of events and the details of the whole events. now I need to just read those events form that calendar. For that I have tried to use the gcode(google code) API which provides the calendar API class. But still I cant read those events.

Edit
Report

3 Answers

24

That code above is pretty awful (and it does not seem to work in ICS - definitely the column names are different)

The page here: http://developer.android.com/guide/topics/providers/calendar-provider.html

provides a much better overview. A (much) simpler code to retrieve calendars:

public class CalendarContentResolver {
    public static final String[] FIELDS = { 
        CalendarContract.Calendars.NAME,
        CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
        CalendarContract.Calendars.CALENDAR_COLOR,
        CalendarContract.Calendars.VISIBLE 
    };

    public static final Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/calendars");

    ContentResolver contentResolver;
    Set<String> calendars = new HashSet<String>();

    public  CalendarContentResolver(Context ctx) {
        contentResolver = ctx.getContentResolver();
    }

    public Set<String> getCalendars() {
        // Fetch a list of all calendars sync'd with the device and their display names
        Cursor cursor = contentResolver.query(CALENDAR_URI, FIELDS, null, null, null);

        try {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String name = cursor.getString(0);
                    String displayName = cursor.getString(1);
                    // This is actually a better pattern:
                    String color = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_COLOR));
                    Boolean selected = !cursor.getString(3).equals("0");
                    calendars.add(displayName);  
                }
            }
        } catch (AssertionError ex) { /*TODO: log exception and bail*/ }

        return calendars;
    }
}

Hope this helps!

answered 2012-05-02T05:26:34.903
20

Ok i found the answer of this whole of the concept that how to use the google calendar application integration with the android phone.

code:--

first you set this line which will goes to read the calendar events form the other class form your class which is current is the ApplicationSettings.java .

ReadCalendar.readCalendar(ApplicationSettings.this);

package com.mycalendarevents.android;

import java.util.Date;
import java.util.HashSet;
import java.util.regex.Pattern;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.format.DateUtils;

public class ReadCalendar
{
    static Cursor cursor;

    public static void readCalendar(Context context) {

        ContentResolver contentResolver = context.getContentResolver();

        // Fetch a list of all calendars synced with the device, their display names and whether the

        cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
                (new String[] { "_id", "displayName", "selected"}), null, null, null);

        HashSet<String> calendarIds = new HashSet<String>();

        try
        {
            System.out.println("Count="+cursor.getCount());
            if(cursor.getCount() > 0)
            {
                System.out.println("the control is just inside of the cursor.count loop");
                while (cursor.moveToNext()) {

                    String _id = cursor.getString(0);
                    String displayName = cursor.getString(1);
                    Boolean selected = !cursor.getString(2).equals("0");

                    System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
                    calendarIds.add(_id);
                }
            }
        }
        catch(AssertionError ex)
        {
            ex.prin
answered 2011-05-30T11:52:08.160
8

I am aware that this is an old post, but I found inspiration for optimizing the solution found in the answer given by Akash Takkar if anyone is in need of a solution in the near future.

The issues


Specically, I found a few issues in the original code:

  1. The loop for retrieving calendar events broke immaturely
    • Hereby, only events from the first calendar was retrieved
  2. The first event in each calendar was skipped by using eventCursor.moveToFirst(); which thereafter moves directly to the next event in the while loop
  3. The id of the calendars were not set correctly in the eventCursor
    • "Calendars._id=" + 1, should be "Calendars._id=" + id,
  4. It would be difficult for others to specify their own time range
  5. The current solution is not object oriented which would hold many advantages
  6. The readability and documentation is not the best

The solution


I have hereby created a Github Library which returns a list of event objects in a specified time range which can be found at:

https://github.com/david-laundav/CalendarService

The source files can be found under "CalendarService/src/dk/CalendarService".

Use cases

The solution itself contains two different methods for different purposes.

First use case:

CalendarService.readCalendar(class.this)
// where class.this is either your class or the context

This method will return a list of events for +/- 1 day

Second use case:

You can also specify your own time range:

CalendarService.readCalendar(class.this, int days, int hours)

An example might be:

CalendarService.readCalendar(class.this, 2, 5)
answered 2013-06-14T13:59:41.137

Your Answer