我正在编写一个允许用户从列表中选择国家的应用程序。当他们选择一个国家时,会出现该国家的国定假日列表。
我见过 CalendarProvider和 Google Calendar V3 ,但它们似乎只提供当前用户的日历。
如何获取特定国家/地区的国定假日?任何建议都会对我有所帮助。
更新:
我找到了this post它可以获取 JSON 格式的假期列表。在此示例中,国家/地区代码由日历 ID 定义,例如 pt_br.brazilian#holiday@group.v.calendar.google.com 或 en.usa#holiday@group.v.calendar.google.com。
但我找不到任何关于此的文档。是否可以通过这种方式使用 API?如果是,我在哪里可以获得这些日历 ID?
最佳答案
使用 Google Calendar API V3 解决的问题.我从 this post 中找到的想法. 假期可以从谷歌的默认假期日历中获取。
默认节假日日历ID列表见here ,支持40个国家。
一段处理权限和获取假期列表的代码:-
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();
}
关于android - 如何获得所选国家的国定假日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18996577/