'Problems with getting all events of selected calendars for today using Calendar Provider
I'm trying to get all events for today from Calendar Provider on the base of selected calendars...
private fun getEventsFromSelectedCalendars(selectedIds: MutableList<Long>) {
eventsListToday.clear()
val calendar = Calendar.getInstance()
calendar.set(2022, Calendar.MAY, 12, 0, 0, 0)
val startDay = calendar.timeInMillis
calendar.set(2022, Calendar.MAY, 12, 23, 59, 59)
val endDay = calendar.timeInMillis
val uri = CalendarContract.Events.CONTENT_URI
val selection = "((${CalendarContract.Events.CALENDAR_ID} = ?) AND (${CalendarContract.Events.DTSTART} >= $startDay) AND (${CalendarContract.Events.DTSTART} <= $endDay))"
for(i in 0 until selectedIds.size) {
val cal = selectedIds[i].toString()
val selectionArgs = arrayOf(cal)
val cur = context.contentResolver.query(
uri,
EVENT_PROJECTION,
selection,
selectionArgs,
null
)
while (cur?.moveToNext() == true) {
val eventId = cur.getLong(PROJECTION_ID_INDEX)
val title = cur.getStringOrNull(PROJECTION_TITLE_INDEX)
val eventLocation = cur.getStringOrNull(PROJECTION_EVENT_LOCATION_INDEX)
val dtStart = cur.getLongOrNull(PROJECTION_DTSTART_INDEX)
val dtEnd = cur.getLongOrNull(PROJECTION_DTEND_INDEX)
val duration = cur.getStringOrNull(PROJECTION_DURATION_INDEX)
val allDay = cur.getIntOrNull(PROJECTION_ALL_DAY_INDEX) == 1
val displayColor = cur.getIntOrNull(PROJECTION_DISPLAY_COLOR_INDEX)
eventsListToday.add(
EventItemModel(
id = eventId,
title = title,
eventLocation = eventLocation,
dtStart = dtStart,
dtEnd = dtEnd,
duration = duration,
allDay = allDay,
displayColor = displayColor
)
)
}
cur?.close()
}
eventsListToday.sortWith (compareBy({ it.dtStart }, { it.allDay }))
}
Finally, I get all the info, except recurring events. I read that for getting recurring events I should use CalendarContract.Instances, but I don't know how to use it correctly in relation to my task, taking into account the selected calendar's ids at the same time.
Also my above code gives allday events with dtStart in accordance with my local time zone (time differs from midnight). I've read allday events begin in UTC time zone. How to correct this in my code?
Could somebody help me, please?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
