Read Calendar Events in Android

Read Calendar Events in Android

Android allows to read calendar events using Calendar Provider. It manages access to a central repository that stores information about calendars, events, attendees, reminders, and other related data. The Calendar Provider is a content provider component.

Application should have access to read calendar events. So, we need to request the READ_CALENDAR permission in the manifest file.

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <uses-permission android:name="android.permission.READ_CALENDAR" />

    <application>
        ...
    </application>

</manifest>

Using the method ContextCompat.checkSelfPermission() we check if required permission was granted. If not, then we request permission by calling the method ActivityCompat.requestPermissions().

A ContentResolver object resolves a URI to the Calendar Provider. The method ContentResolver.query() returns a Cursor. We iterate through the Cursor using the method moveToNext() to retrieve a list of event title, the time when the event starts and ends. If event was deleted, then deleted column is equal to 1.

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.CalendarContract
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity()
{
    private val permission: String = Manifest.permission.READ_CALENDAR
    private val requestCode: Int = 1

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (ContextCompat.checkSelfPermission(this, permission)
            != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
        } else {
            readCalendarEvents()
        }
    }

    private fun readCalendarEvents()
    {
        val titleCol = CalendarContract.Events.TITLE
        val startDateCol = CalendarContract.Events.DTSTART
        val endDateCol = CalendarContract.Events.DTEND

        val projection = arrayOf(titleCol, startDateCol, endDateCol)
        val selection = CalendarContract.Events.DELETED + " != 1"

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

        val titleColIdx = cursor!!.getColumnIndex(titleCol)
        val startDateColIdx = cursor.getColumnIndex(startDateCol)
        val endDateColIdx = cursor.getColumnIndex(endDateCol)

        val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US)

        while (cursor.moveToNext()) {
            val title = cursor.getString(titleColIdx)
            val startDate = formatter.format(Date(cursor.getLong(startDateColIdx)))
            val endDate = formatter.format(Date(cursor.getLong(endDateColIdx)))

            Log.d("MY_APP", "$title $startDate $endDate")
        }

        cursor.close()
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.