使用Event对象将事件添加到Google日历

时间:2015-12-07 15:47:11

标签: android calendar google-calendar-api

我想将活动添加到我的应用内的Google日历中。我按照教程创建了事件,但最后还有一个"服务"。它到底在哪里来自?

我的代码是:

Event event = new Event();
    event.setSummary(this.title.getText().toString());
    if ( !this.location.getText().toString().equals("") || this.location.getText() != null) {
        event.setLocation(this.location.getText().toString());
    }

    Long startDate = Long.parseLong(this.date_start.getText().toString()) +
            Long.parseLong(this.time_start.getText().toString());
    DateTime startDateTime = new DateTime(
            Long.parseLong(this.date_start.getText().toString()) +
            Long.parseLong(this.time_start.getText().toString())
    );
    DateTime endDateTime = new DateTime(
            Long.parseLong(this.date_end.getText().toString()) +
            Long.parseLong(this.time_end.getText().toString())
    );

    EventDateTime start = new EventDateTime();
    start.setDateTime(startDateTime);
    start.setTimeZone("Europe/Berlin");  //TODO: replace with timezone by geolocation
    EventDateTime end = new EventDateTime();
    end.setDateTime(endDateTime);
    end.setTimeZone("Europe/Berlin");  //TODO: replace with timezone by geolocation

    event.setStart(start);
    event.setEnd(end);

    EventReminder[] reminderOverrides = new EventReminder[] {
            new EventReminder().setMethod("popup").setMinutes(7*24*60),
            new EventReminder().setMethod("popup").setMinutes(24*60),
            new EventReminder().setMethod("popup").setMinutes(60),
    };

    Event.Reminders reminders = new Event.Reminders();
    reminders.setUseDefault(false);
    reminders.setOverrides(Arrays.asList(reminderOverrides));
    event.setReminders(reminders);

    String calendarId = "primary";
    event = service.events().insert(calendarId, event).execute();

教程如下:click me

有人可以帮帮我吗?

干杯

1 个答案:

答案 0 :(得分:0)

在教程的最顶部说:

// Refer to the Java quickstart on how to setup the environment:
// https://developers.google.com/google-apps/calendar/quickstart/java
// Change the scope to CalendarScopes.CALENDAR and delete any stored
// credentials.

该服务是作为其一部分创建的。