使用xamarin谷歌日历android检索公共假期

时间:2018-01-13 10:24:03

标签: c# android xamarin xamarin.android calendar

我想使用Xamarin和Google日历创建一个示例应用程序。

我的计划是使用谷歌日历检索公众假期列表,例如:“en.usa#holiday@group.v.calendar.google.com”

因为,我是Xamarin的新手,我试图模仿https://stackoverflow.com/a/19048193/3125120回答的行为:

com.google.api.services.calendar.Calendar client = null;
        credential = GoogleAccountCredential.usingOAuth2(mContext, CalendarScopes.CALENDAR);
        credential.setSelectedAccountName(mList.get(0));
        client = getCalendarService(credential);
        do {
            com.google.api.services.calendar.model.Events events;
            events = client.events().list("en.usa#holiday@group.v.calendar.google.com").setPageToken(pageToken).execute();
            onHolidayChecked(events.getItems()); //result return here (events.getItems())
            pageToken = events.getNextPageToken();
        } while (pageToken != null);

private com.google.api.services.calendar.Calendar getCalendarService(GoogleAccountCredential credential) {
    return new com.google.api.services.calendar.Calendar.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
}

有人可以提出一种模仿相同行为的方法吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我尝试了下面的代码,它只是从日历中返回假期列表。

var calendarsUri = CalendarContract.Events.ContentUri;
var cursor = Forms.Context.ContentResolver.Query(calendarsUri, null, null, null, null);

if (cursor.MoveToFirst())
            {
                do
                {
                      calendarDataList.Add(new Calendar()
                      {
                          Id = cursor.GetString(cursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.Id)),
                          CalendarDisplayName = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName)),
                          AccountName = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Calendars.InterfaceConsts.AccountType)),
                          Title = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Title)),
                          Description = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Description)),
                          Dtstart = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Dtstart)),
                          Dtend = cursor.GetString(cursor.GetColumnIndex(CalendarContract.Events.InterfaceConsts.Dtend))                       
                      }); 
                 } while (cursor.MoveToNext()); 
             }

calendarDataList包含所有公共假期。