I trying to add event in the calendar using calendar provider which is working fine on all SDK Versions except SDK Version 34 (android 14). This is majorly happening on devices (primarily Google Pixel) where the default calendar app is Google Calendar.
I am mentioning the code below.
val values = ContentValues().apply {
put(CalendarContract.Events.DTSTART, dtStart)
put(CalendarContract.Events.DURATION, "PT05M")
put(CalendarContract.Events.RRULE, "FREQ=DAILY;COUNT=10")
put(CalendarContract.Events.TITLE, config.title)
put(CalendarContract.Events.DESCRIPTION, "${config.description} $shareLink")
put(CalendarContract.Events.EVENT_LOCATION, "Location")
put(CalendarContract.Events.CALENDAR_ID, 1L)
put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id)
}
val insertUri = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, values)
I have tried with different calendar id values like 2,3,4 etc.
Have also tried to first fetch available calendars and then fetch id of first calendar and then add event in that calendar.
Below is the code for fetching list of calendars.
private val calendarProjection: Array<String> = arrayOf(
CalendarContract.Calendars._ID, // 0
CalendarContract.Calendars.ACCOUNT_NAME, // 1
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2
CalendarContract.Calendars.OWNER_ACCOUNT // 3
)
companion object {
private const val CALENDAR_ACCESS = "CALENDAR_ACCESS"
// The indices for the calendar projection array above.
private const val PROJECTION_ID_INDEX = 0
private const val PROJECTION_ACCOUNT_NAME_INDEX = 1
private const val PROJECTION_DISPLAY_NAME_INDEX = 2
private const val PROJECTION_OWNER_ACCOUNT_INDEX = 3
}
private fun fetchCalendarList(): List<CalendarEntry> {
val calendarList = mutableListOf<CalendarEntry>()
context.contentResolver.query(CalendarContract.Calendars.CONTENT_URI, calendarProjection, null, null, null)?.let { cursor ->
while (cursor.moveToNext()) {
calendarList.add(
CalendarEntry(
id = cursor.getLong(PROJECTION_ID_INDEX),
displayName = cursor.getString(PROJECTION_DISPLAY_NAME_INDEX),
accountName = cursor.getString(PROJECTION_ACCOUNT_NAME_INDEX),
ownerAccount = cursor.getString(PROJECTION_OWNER_ACCOUNT_INDEX)
)
)
}
cursor.close()
}
if (calendarList.isEmpty()) {
calendarList.add(CalendarEntry(1, "", "", ""))
}
return calendarList
}
The expected behaviour / result should be that it should add the event in the calendar app.