How to read google calendar events in android?

Anand picture Anand · Nov 27, 2013 · Viewed 16.4k times · Source

I am reading all the android calendar events and it is working fine by using this below code but, I want to fetch all the google calendar events from user mail accounts .How can I do that? Any suggestions or sample program will be really helpfull!

For example: I want to get the events for [email protected] (the mail account which I opened in android mobile)

public static void readCalendarEvent(Context context) throws ParseException {
        ContentResolver contentResolver = context.getContentResolver();
        Calendar calendar = Calendar.getInstance();
        String dtstart = "dtstart";
        String dtend = "dtend";


        SimpleDateFormat    displayFormatter = new SimpleDateFormat("MMMM dd, yyyy (EEEE)");

         stime=displayFormatter.format(calendar.getTime());     

        SimpleDateFormat startFormatter = new SimpleDateFormat("MM/dd/yy");
        String dateString = startFormatter.format(calendar.getTime());

        long after = calendar.getTimeInMillis();
        SimpleDateFormat formatterr = new SimpleDateFormat("hh:mm:ss MM/dd/yy");
        Calendar endOfDay = Calendar.getInstance();
        Date dateCCC = formatterr.parse("47:59:59 " + dateString);
        endOfDay.setTime(dateCCC);



        cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"), (new String[] { "calendar_id", "title", "description", "dtstart", "dtend","eventTimezone", "eventLocation" }), "(" + dtstart + ">" + after + " and " + dtend + "<" + endOfDay.getTimeInMillis() + ")", null, "dtstart ASC");
        gCalendar = new ArrayList<GoogleCalendar>();
        try {


            if (cursor.getCount() > 0) {


                while (cursor.moveToNext()) {
                    GoogleCalendar googleCalendar = new GoogleCalendar();
                    gCalendar.add(googleCalendar);
                    int calendar_id = cursor.getInt(0);
                    googleCalendar.setCalendar_id(calendar_id);
                    String title = cursor.getString(1);
                    googleCalendar.setTitle(title);
                    String description = cursor.getString(2);
                    googleCalendar.setDescription(description);
                    String dtstart1 = cursor.getString(3);
                     dt=convertDate(dtstart1,"hh:mm:ss");

                    googleCalendar.setDtstart(dt);                  

                String dtend1 = cursor.getString(4);
                    googleCalendar.setDtend(dtend1);


                    String eventTimeZone=cursor.getString(5);
                    googleCalendar.setEventTimeZone(eventTimeZone);
                    String eventlocation = cursor.getString(6);

                    googleCalendar.setEventlocation(eventlocation);



                }
            }
        } catch (AssertionError ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Answer

sromku picture sromku · Aug 19, 2015

This post is a little bit old, but here is another easy solution for getting data related to Calendar content provider in Android:

Use this lib: https://github.com/EverythingMe/easy-content-providers

And now, get all calendars:

CalendarProvider calendarProvider = new CalendarProvider(context);
List<Calendar> calendars = calendarProvider.getCalendars().getList();

Or, get all Events of specific calendar:

List<Event> calendars = calendarProvider.getEvents(calendar.id).getList();

And there is also option to get Reminders, Attendees, Instances.

It works with lists or cursor and there a sample app to see how it looks and works. In fact, there is support for all Android content providers like: Contacts, SMS, Calls, ... Full doc with all options: https://github.com/EverythingMe/easy-content-providers/wiki/Android-providers

Hope it helped :)