Unable to Retrieve Reminder Details from Device Calendar

痴心易碎 提交于 2021-02-08 06:58:58

问题


I am able to retrieve all the events from the device calendar from the below code,

 @SuppressLint("MissingPermission")
    private fun getAccountCalendarEvents(selectedEmail: String) {
        var count = 0
        val projectionArray =
            arrayOf(
                CalendarContract.Calendars._ID,
                CalendarContract.Events.TITLE,
                CalendarContract.Events.DESCRIPTION,
                CalendarContract.Events.DTSTART,
                CalendarContract.Events.DTEND,
                CalendarContract.Events.ALL_DAY,
                CalendarContract.Events.EVENT_LOCATION,
                CalendarContract.Attendees.ORGANIZER,
                CalendarContract.Events.EVENT_TIMEZONE,
                CalendarContract.Events.ACCOUNT_TYPE,
                CalendarContract.Calendars.ACCOUNT_NAME
            )

        val startTime = Calendar.getInstance()
        startTime.set(Calendar.HOUR_OF_DAY, 0)
        startTime.set(Calendar.MINUTE, 0)
        startTime.set(Calendar.SECOND, 0)
        startTime.add(Calendar.YEAR, -2)

        val endTime = Calendar.getInstance()
        endTime.add(Calendar.YEAR, 2)

        val selection =
            "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.timeInMillis + " ) AND ( " + CalendarContract.Events.DTEND + " <= " + endTime.timeInMillis + " ) AND ( deleted != 1 )  AND (account_name = '" + selectedEmail + "'))"

        val cursor = contentResolver.query(
            CalendarContract.Events.CONTENT_URI,
            projectionArray,
            selection,
            null,
            null
        )

        if (cursor != null && cursor.count > 0 && cursor.moveToFirst()) {
            do {
                var calendarString = ""

                val calendarId = "Id: " + cursor.getInt(0)
                val title = "Title: " + cursor.getString(1)
                val description = "Description: " + cursor.getString(2)
                val startDate = "Start Date: " + cursor.getString(3)
                val endDate = "End Date: " + cursor.getString(4)
                val allDay = "All Day: " + cursor.getString(5)
                val location = "Location: " + cursor.getString(6)
                val organizer = "Organizer: " + cursor.getString(7)
                val timeZone = "Timezone: " + cursor.getString(8)
                val accountType = "Account Type: " + cursor.getString(9)
                val accountName = "Account Name: " + cursor.getString(10)

                calendarString =
                    calendarId + "\n" + title + "\n" + description + "\n" + startDate + "\n" + endDate + "\n" + allDay + "\n" + location + "\n" + organizer + "\n" + timeZone + "\n" + accountType + "\n" + accountName

                result = if (result.isEmpty()) {
                    calendarString
                } else {
                    result + "\n\n\n" + calendarString
                }
                count++
            } while (cursor.moveToNext())
        }
//        txtResults.text = result
        Log.e("DCal", "" + count)
        getEventsReminders(selectedEmail)

    }

This works absolutely fine and perfect.Whereas, when I'm trying to fetch the Reminders from the device with the following code,

 @SuppressLint("MissingPermission")
    private fun getAccountCalendarReminders(selectedEmail: String) {
        var count = 0
        val projectionArray =
            arrayOf(
                CalendarContract.Reminders.EVENT_ID,
                CalendarContract.Reminders.METHOD,
                CalendarContract.Reminders.MINUTES
            )

        val startTime = Calendar.getInstance()
        startTime.add(Calendar.YEAR, -2)

        val endTime = Calendar.getInstance()
        endTime.add(Calendar.YEAR, 2)

//        val selection =
//            "((account_name = '$selectedEmail'))"


        val cursor = contentResolver.query(
            CalendarContract.Reminders.CONTENT_URI,
            projectionArray,
            null,
            null,
            null
        )


        if (cursor != null && cursor.count > 0 && cursor.moveToFirst()) {
            do {
                var calendarString = ""

                val calendarId = "Id: " + cursor.getInt(0)
                val title = "Method: " + cursor.getString(1)
                val description = "Minutes: " + cursor.getString(2)
//                val startDate = "Title: " + cursor.getString(3)
//                val endDate = "End Date: " + cursor.getString(4)
//                val allDay = "All Day: " + cursor.getString(5)
//                val location = "Location: " + cursor.getString(6)
//                val organizer = "Organizer: " + cursor.getString(7)
//                val timeZone = "Timezone: " + cursor.getString(8)
//                val accountType = "Account Type: " + cursor.getString(9)
//                val accountName = "Account Name: " + cursor.getString(10)
                calendarString =
                    calendarId + "\n" + title + "\n" + description


                result = if (result.isEmpty()) {
                    calendarString
                } else {
                    result + "\n\n\n" + calendarString
                }
                count++
            } while (cursor.moveToNext())
        }

        Log.e("DRem", "" + count)
        txtResults.text = result

    }

And the output of this Reminders are just,

Could anyone please help me in getting the Reminders Information completely like, 1. Reminder Name, 2. Start Date, 3. Location, etc. the same fields which Events do have.

Note: I have gone through this library as well,

https://github.com/EverythingMe/easy-content-providers

This library does the same. It doesn't list the reminders from the device calendar.

Code Snippet from the above Lib:

val provider = CalendarProvider(this)
val events = provider.getEvents(id.toLong()).list as ArrayList<Event>
val reminders = provider.getReminders(id.toLong()).list as ArrayList<Event>

来源:https://stackoverflow.com/questions/60812072/unable-to-retrieve-reminder-details-from-device-calendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!