在MainActivity类中,我有一个AsyncTask内部类,用于从日历中检索事件:
private class MakeRequestTask extends AsyncTask<Void, Void, List<>> {
private com.google.api.services.calendar.Calendar mService;
private Exception mLastError = null;
MakeRequestTask(GoogleAccountCredential credential) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName(getString(R.string.app_name))
.build();
}
@Override
protected List<> doInBackground(Void... params) {
try {
return getDataFromApi();
} catch (Exception e) {
mLastError = e;
cancel(true);
return null;
}
}
private List<> getDataFromApi() throws IOException {
DateTime now = new DateTime(System.currentTimeMillis());
List<> eventDates = new ArrayList<>();
Events events = mService.events().list("primary")
.setMaxResults(30)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
String sameDateTime = null;
boolean showDate;
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
eventDates.add(new AppointmentItem(event.getId(),
start,
event.getSummary(),
event.getLocation())
);
}
return eventDates;
}
}
如果在列表方法中我将“主”作为日历ID Events events = mService.events().list("primary")
传递,则效果很好。
这是默认设置,但我想从与特定帐户相关联的每个日历中检索事件。
因此我四处寻找,发现CalendarList calendarList = mService.calendarList().list().execute();
可以从日历中获取所有数据(包括日历的ID),但是如果将ID放在上一个语句中,则不会得到任何事件。
我该如何解决?