简体   繁体   English

如何在Android的Google Calendar API中创建事件

[英]How to create Events in Google Calendar API with Android

I´m trying to insert some events in my Google Calendar with Android. 我正在尝试在Android版Google日历中插入一些事件。 For reading the calendar, there is a quickstart tutorial for Android on the Googles developer page and it works fine. 要阅读日历,请在Google的开发人员页面上找到Android的快速入门教程,它可以正常运行。 But there is no Android code example for inserting events, I just found an Java code example: 但是没有用于插入事件的Android代码示例,我只是找到了一个Java代码示例:

    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? 是否可以导入“服务”,所以我可以使用Java代码在Android中插入事件,还是必须使用其他完全方法创建事件?

Link to Googles introduction for creating events: https://developers.google.com/google-apps/calendar/create-events 链接到Google创建事件的简介: https : //developers.google.com/google-apps/calendar/create-events

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. 如果您已阅读文档并设置了Calendar Activity类,则应该已经使用所需的权限初始化了mCredential对象。 Then all you have to do is call that simple java code which was provided to create an event. 然后,您要做的就是调用提供的创建事件的简单Java代码。 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. 另一件事是,您必须通过AsyncTask调用此方法。 Cause creating an event is a background process. 原因创建事件是一个后台过程。 And i hope this method also clears you problem of cannot resolve symbol service . 并且我希望这种方法也可以解决无法解决符号服务的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM