问题
I'm creating a watchface for Android Wear which will display calendar events. Based on this page (and the WatchFace
sample provided in the SDK), I managed to query the next events for the day, and display them on my watchface (below is the code I use to query the events).
The problem is that recurring events are not returned in the cursor, and thus are not displayed on the watch face. Is there any parameter to add in the query to get recurring events ?
private static final String[] PROJECTION = {
CalendarContract.Calendars._ID, // 0
CalendarContract.Events.DTSTART, // 1
CalendarContract.Events.DTEND, // 2
CalendarContract.Events.DISPLAY_COLOR, // 3
};
protected List<SpiralEvent> queryEvents() {
// event is a custom POJO object
List<Event> events = new ArrayList<>();
long begin = System.currentTimeMillis();
Uri.Builder builder = WearableCalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, begin);
ContentUris.appendId(builder, begin + DateUtils.DAY_IN_MILLIS);
final Cursor cursor = mService.getContentResolver()
.query(builder.build(),
PROJECTION,
null, // selection (all)
null, // selection args
null); // order
// get the start and end time, and the color
while (cursor.moveToNext()) {
long start = cursor.getLong(1);
long end = cursor.getLong(2);
int color = cursor.getInt(3);
events.add(new Event(start, end, color));
}
cursor.close();
return events;
}
回答1:
You have to use CalendarContract.Instances.BEGIN
instead of CalendarContract.Events.DTSTART
; thus, you can change the PROJECTION
to:
private static final String[] PROJECTION = {
CalendarContract.Calendars._ID, // 0
CalendarContract.Events.BEGIN, // 1
CalendarContract.Events.END, // 2
CalendarContract.Events.DISPLAY_COLOR, // 3
};
The reason is that:
Events.DTSTART
returns start time of the original created event. Note that this event is often in the past; thus, it's filtered out.Events.BEGIN
returns start time of each instance of the event.
Check out source in CalendarEvent.java from my github sample project https://github.com/mtrung/android-WatchFace.
来源:https://stackoverflow.com/questions/29565075/wearablecalendarcontract-query-doesnt-return-recurring-events