Snowman
Snowman

Reputation: 179

How to create Events in Google Calendar API with Android

I´m trying to insert some events in my Google Calendar with Android. For reading the calendar, there is a quickstart tutorial for Android on the Googles developer page and it works fine. But there is no Android code example for inserting events, I just found an Java code example:

    Event event = new Event()
            .setSummary("Google I/O 2015")
            .setLocation("800 Howard St., San Francisco, CA 94103")
            .setDescription("A chance to hear more about Google's developer products.");

    DateTime startDateTime = new DateTime("2015-05-28T09:00:00-07:00");
    EventDateTime start = new EventDateTime()
            .setDateTime(startDateTime)
            .setTimeZone("America/Los_Angeles");
    event.setStart(start);

    DateTime endDateTime = new DateTime("2015-05-28T17:00:00-07:00");
    EventDateTime end = new EventDateTime()
            .setDateTime(endDateTime)
            .setTimeZone("America/Los_Angeles");
    event.setEnd(end);

    String calendarId = "primary";
    event = service.events().insert(calendarId, event).execute();
    System.out.printf("Event created: %s\n", event.getHtmlLink());

The problem: 'cannot resolve symbol 'service''. Is there a possibility to import 'service', so I can use the Java Code for inserting Events with Android or do I have to use a completly other way for creating events?

Link to Googles introduction for creating events: https://developers.google.com/google-apps/calendar/create-events

Upvotes: 3

Views: 6970

Answers (1)

Abir Hasan Shawon
Abir Hasan Shawon

Reputation: 6119

I'm really late to this question. I don't know if you have already found a way. But i'm just going to try to give a solution to this. If you have read the documentation and setup the class of Calendar Activity then you should have already got the mCredential object initialized with permissions which are required. Then all you have to do is call that simple java code which was provided to create an event. I'm providing a sample method to do that:

public void createEvent(GoogleAccountCredential mCredential) {

    HttpTransport transport = AndroidHttp.newCompatibleTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    com.google.api.services.calendar.Calendar service = new com.google.api.services.calendar.Calendar.Builder(
            transport, jsonFactory, mCredential)
            .setApplicationName("R_D_Location Callendar")
            .build();


    Event event = new Event()
            .setSummary("Event- April 2016")
            .setLocation("Dhaka")
            .setDescription("New Event 1");

    DateTime startDateTime = new DateTime("2016-04-17T18:10:00+06:00");
    EventDateTime start = new EventDateTime()
            .setDateTime(startDateTime)
            .setTimeZone("Asia/Dhaka");
    event.setStart(start);

    DateTime endDateTime = new DateTime("2016-04-17T18:40:00+06:00");
    EventDateTime end = new EventDateTime()
            .setDateTime(endDateTime)
            .setTimeZone("Asia/Dhaka");
    event.setEnd(end);

    String[] recurrence = new String[]{"RRULE:FREQ=DAILY;COUNT=2"};
    event.setRecurrence(Arrays.asList(recurrence));

    EventAttendee[] attendees = new EventAttendee[]{
            new EventAttendee().setEmail("[email protected]"),
            new EventAttendee().setEmail("[email protected]"),
    };
    event.setAttendees(Arrays.asList(attendees));

    EventReminder[] reminderOverrides = new EventReminder[]{
            new EventReminder().setMethod("email").setMinutes(24 * 60),
            new EventReminder().setMethod("popup").setMinutes(10),
    };
    Event.Reminders reminders = new Event.Reminders()
            .setUseDefault(false)
            .setOverrides(Arrays.asList(reminderOverrides));
    event.setReminders(reminders);

    String calendarId = "primary";
    try {
        event = service.events().insert(calendarId, event).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.printf("Event created: %s\n", event.getHtmlLink());

}    

And another thing is that you have to call this method through an AsyncTask. Cause creating an event is a background process. And i hope this method also clears you problem of cannot resolve symbol service.

Upvotes: 4

Related Questions