如何在科特林中获取日历事件?

how to get event of calendar in kotlin?

如何获取我在 kotlin 中添加到日历中的日历事件? 我想要日历,然后当我们回来时从日历中获取事件并将其显示给我们,我们该怎么做?

这就是我制作日历的方式:


private fun addEventOnCalendar(jdn: Jdn) {
        val activity = this ?: return
        if (ActivityCompat.checkSelfPermission(
                activity,
                Manifest.permission.READ_CALENDAR
            ) != PackageManager.PERMISSION_GRANTED
        )
            activity.askForCalendarPermission()
        else {
            runCatching { addEvent.launch(jdn) }.onFailure(logException).onFailure {

            }
        }
    }


    private val addEvent =
        registerForActivityResult(object : ActivityResultContract<Jdn, Void?>() {
            override fun parseResult(resultCode: Int, intent: Intent?): Void? = null
            override fun createIntent(context: Context, input: Jdn): Intent {
                val time = input.toJavaCalendar().timeInMillis
                return Intent(Intent.ACTION_INSERT)
                    .setData(CalendarContract.Events.CONTENT_URI)
                    .putExtra(CalendarContract.Events.DESCRIPTION, "DESCRIPTION")
                    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, time)
                    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, time)
                    .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
            }
        }) {

        }

我终于找到了解决方案:

@SuppressLint("LongLogTag")
  fun readDeviceEvents(
    context: Context, startingDate: Calendar, rangeInMillis: Long
): List<Event> = if (
    ActivityCompat.checkSelfPermission(
        context, Manifest.permission.READ_CALENDAR
    ) != PackageManager.PERMISSION_GRANTED
) emptyList() else runCatching {
     context.contentResolver.query(
        CalendarContract.Instances.CONTENT_URI.buildUpon().apply {
            ContentUris.appendId(this, startingDate.timeInMillis - DateUtils.DAY_IN_MILLIS)
            ContentUris.appendId(
                this,
                startingDate.timeInMillis + rangeInMillis + DateUtils.DAY_IN_MILLIS
            )
        }.build(), arrayOf(
            CalendarContract.Instances.EVENT_ID, // 0
            CalendarContract.Instances.TITLE, // 1
            CalendarContract.Instances.DESCRIPTION, // 2
            CalendarContract.Instances.BEGIN, // 3
            CalendarContract.Instances.END, // 4
            CalendarContract.Instances.VISIBLE, // 5
            CalendarContract.Instances.ALL_DAY, // 6
            CalendarContract.Instances.EVENT_COLOR // 7
        ), null, null, null
    )?.use {
        Log.i(TAG, "readDeviceEvents use cursor: ")
        generateSequence { if (it.moveToNext()) it else null }.filter {
            Log.i(TAG, "readDeviceEvents filter cursor: ")
            it.getString(5) == "1" // is visible
        }.map {
            Log.i(TAG, "readDeviceEvents map cursor: ")
            val id = it.getInt(0)
            val startDate = Date(it.getLong(3))
            val endDate = Date(it.getLong(4))
            val description = it.getString(2)
            val title = if (it.getString(6) == "1") "\uD83D\uDCC5 ${it.getString(1) ?: ""}"
            else "\uD83D\uDD53 ${it.getString(1) ?: ""} "
            val startCalendar = startDate
            val endCalendar = endDate

            Log.i(TAG, "readDeviceEvents id: " + id)
            Log.i(TAG, "readDeviceEvents startDate: " + startDate)
            Log.i(TAG, "readDeviceEvents endDate: " + endDate)
            Log.i(TAG, "readDeviceEvents startCalendar: " + startCalendar)
            Log.i(TAG, "readDeviceEvents endCalendar: " + endCalendar)
            Log.i(TAG, "readDeviceEvents description: " + description)
            Log.i(TAG, "readDeviceEvents title: " + title)

            Event(id=id.toLong(),title= title,desc= description)

        }.take(1000 /* let's put some limitation */).toList()
    }
}.onFailure(logException).getOrNull() ?: emptyList()