How to read google calendar events in android?
Asked Answered
B

2

14

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();
        }
    }
}
Boccherini answered 27/11, 2013 at 7:32 Comment(1)
Have you checked this site - live.temboo.com/library/Library/Google/Calendar <br/> It may help you. Ref#SO- #15439106Bushy
A
5

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 :)

Adara answered 19/8, 2015 at 15:23 Comment(3)
Recurrent event issueEpimenides
@Adara it doesn't seem to implement setters... Great lib thoughTrappings
Could someone please post a complete example for this?Heliotropism
N
0

You can read this page. It is written that how you can get the calendar information.

Speaking simply,

  1. get the account logged in the android device. (return object is Account.)
  2. get the calendar list the specific has.
  3. get the events in the range of the particular time. (it is for one calendar.)
  4. repeat 3 for all calendars.
Nashoma answered 28/11, 2013 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.