如何在Google日历上插入活动

时间:2018-03-13 21:55:58

标签: android google-calendar-api

我目前在我的应用中有一项功能可将事件插入Google日历。但是,此功能的代码已超过4年,今天,当我测试此应用时,我发现该事件未插入到Google日历中。但是,它位于普通的Android日历上,但是当我访问我的Google帐户时,您创建的活动未列出。

执行此类插入的代码是:

public class CalendarEventCreator 
{
    private String userAccount,timeZone,accountName,displayName;
    private long calendarID;
    private boolean haveMainCalendar;
    /*
     * Columns of the Calendar's table
     * */
    private static final String[] COLUMNS = new String[]
    {
        Calendars._ID,                           // 0
        Calendars.ACCOUNT_NAME,                  // 1
        Calendars.CALENDAR_DISPLAY_NAME,         // 2
        Calendars.OWNER_ACCOUNT,                 // 3
        Calendars.CALENDAR_TIME_ZONE             // 4
    };


    private void initializer()
    {
        haveMainCalendar = false;
        userAccount = null;
        accountName = null;
        displayName = null;
        timeZone = null;
        calendarID =-1;
    }
    /*
     * Search for the first gmail account
     * TODO:support multiple google accounts on the same device
     * */
    private void getUserMainCalendar(Context ctx)
    {
        ContentResolver cr = ctx.getContentResolver();
        initializer();

        Cursor cur = null;
        cur = cr.query(Calendars.CONTENT_URI, COLUMNS,null,null,null);
        while(cur.moveToNext())
        {
            if (cur.getString(3).contains("@gmail.com"))
            {
                haveMainCalendar = true;
                calendarID = cur.getLong(0);
                accountName = cur.getString(1);
                displayName = cur.getString(2);
                userAccount = cur.getString(3);
                timeZone = cur.getString(4);
                break;
            }
        }
    }


    public void addEventCalendar(Reminder reminder, Context ctx) throws CalendarNotFoundException, 
                                                                        ParseException
    {
        long startMilliseconds,endmilliseconds;
        Calendar calEnd;
        startMilliseconds = endmilliseconds = 0;

        getUserMainCalendar(ctx);

        if(haveMainCalendar)
        {
            ContentResolver cr = ctx.getContentResolver();
            calEnd = Calendar.getInstance();
            Date date = DateFormat.dateFormater(reminder.getDate()+" "+reminder.getHour());
            calEnd.setTime(date);

            startMilliseconds = calEnd.getTimeInMillis()-100000;
            endmilliseconds = calEnd.getTimeInMillis();

            ContentValues values = new ContentValues();

            values.put(Events.DTSTART,startMilliseconds);
            values.put(Events.DTEND,endmilliseconds);
            values.put(Events.TITLE,reminder.getText());
            values.put(Events.DESCRIPTION,reminder.getCategory().getName()+" - PositivoApp");
            values.put(Events.CALENDAR_ID,calendarID);
            values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());


            cr.insert(Events.CONTENT_URI, values);
        }
        else
            throw new CalendarNotFoundException();
    }
}

SKD版本是:

 <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />

这段代码有什么问题吗?

2 个答案:

答案 0 :(得分:0)

将您的OAuth范围设置为https://www.googleapis.com/auth/calendar。 确保经过身份验证的用户具有您提供的calendarId对日历的写访问权限(例如,通过调用calendarId的calendarList.get()并检查accessRole)。

答案 1 :(得分:0)

Events.insert上有一个java示例,其中显示了如何添加事件:

greaterThan

如前所述,不要忘记将范围设置为 // Refer to the Java quickstart on how to setup the environment: // https://developers.google.com/calendar/quickstart/java // Change the scope to CalendarScopes.CALENDAR and delete any stored // credentials. 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[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"}; event.setRecurrence(Arrays.asList(recurrence)); EventAttendee[] attendees = new EventAttendee[] { new EventAttendee().setEmail("lpage@example.com"), new EventAttendee().setEmail("sbrin@example.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"; event = service.events().insert(calendarId, event).execute(); System.out.printf("Event created: %s\n", event.getHtmlLink()); 。如果您要更改范围,请删除以前保存的.json凭据以使新范围生效。