以格式创建的日历未保存

时间:2016-09-09 16:52:34

标签: android calendar google-calendar-api android-syncadapter calendarcontract

我正在尝试使用this topic

中的代码创建日历
'Erd\xC3\xA4pfel'.decode('unicode-escape').encode('utf-8').decode('utf-8')

然后拨打电话:

 private static void  createCalendar( Context mContext, Account account)
{
    final ContentValues v = new ContentValues();
    v.put(CalendarContract.Calendars.NAME,"TEST");
    v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "TEST");
    v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
    v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
    v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN);
    v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
    v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
   // v.put(CalendarContract.Calendars._ID, 123);
    v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
    v.put(CalendarContract.Calendars.VISIBLE, 1);
    Uri creationUri = asSyncAdapter(CalendarContract.Calendars.CONTENT_URI, account.name, account.type);
    Uri calendarData = mContext.getContentResolver().insert(creationUri, v);
    long id = Long.parseLong(calendarData.getLastPathSegment());
}
private static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
    return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter
            (CalendarContract.Calendars.ACCOUNT_NAME,account)
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType) .build();

}

然后我从createCalendar(c, AccountManager.get(c).getAccountsByType("com.google")[0]); // i've checked - it's correct account to add calendar

中检索数据
ContentResolver

并在光标中查看新日历。但是:

  • 它没有出现在google accound

  • 它会在一段时间后消失(猜测,因为同步)

在5.1.1上测试

如何以程序方式为我的帐户创建日历?

1 个答案:

答案 0 :(得分:1)

尝试使用calendar.insert创建另一个日历。

由于您使用的是Java,我认为此代码段可能会有所帮助:

import com.google.api.services.calendar.Calendar;

// ...

// Initialize Calendar service with valid OAuth credentials
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials)
    .setApplicationName("applicationName").build();

// Create a new calendar
com.google.api.services.calendar.model.Calendar calendar = new Calendar();
calendar.setSummary("calendarSummary");
calendar.setTimeZone("America/Los_Angeles");

// Insert the new calendar
Calendar createdCalendar = service.calendars().insert(calendar).execute();

System.out.println(createdCalendar.getId());

Try-it干运行以查看它是否有效。