3

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

Snowman
  • 169
  • 4
  • 17
  • Did you include the packages com.google.api.services.calendar com.google.api.services.calendar.model in your code? you can also check sample java codes here https://developers.google.com/api-client-library/java/apis/calendar/v3 .Also, check this link for android code http://developer.android.com/guide/topics/providers/calendar-provider.html#intent-insert – SGC Sep 15 '15 at 20:34
  • If you used the quickstart as your basis then service is called mService. Try replacing it everywhere ;) – luc Sep 15 '15 at 21:02
  • @SGC Yes, I included the packages and also imported the jar´s, like suggested in your first link. I have also checked your second link before and tried it, the problem here is the code is made for the local Android Calendar Application, so I can create events for this calendar, but dont know how to change the code so I can create events for the Google calendar. – Snowman Sep 16 '15 at 14:19
  • @luc : "Cannot resolve symbol mService" – Snowman Sep 16 '15 at 14:20
  • I propose you post your entire code. – luc Sep 16 '15 at 14:42
  • Edit (was not possible to change my comment anymore -.-)@luc : I looked it up in my MainActivity, there was a variable named mService and the type was com.google.api.services.calendar.Calendar ... okay, so i solved the problem with the "Cannot resolve symbol mService", it´s still not working, but at least I took a step forward, Thank you :) – Snowman Sep 16 '15 at 14:48
  • Cool, thanks for letting me know! :) – luc Sep 22 '15 at 17:06

1 Answers1

4

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("abir@aksdj.com"),
            new EventAttendee().setEmail("asdasd@andlk.com"),
    };
    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.

Abir Hasan Shawon
  • 6,069
  • 5
  • 17
  • 28
  • 4
    A late answer is better than no answer, thank you. I don´t need it anymore, but maybe it will help other people. – Snowman Apr 18 '16 at 09:02
  • can you help me here http://stackoverflow.com/questions/36885407/adding-events-for-all-days-between-start-date-and-end-date-in-google-calendar-an – Elizabeth May 12 '16 at 06:32