Read Call Logs in Android

Read Call Logs in Android

Android allows to read call logs using Call Log Provider. It manages access to a central repository that stores information about places and received calls. The Call Log Provider is a content provider component.

Application should have access to read call logs. So, we need to request the READ_CALL_LOG 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_CALL_LOG" />

    <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 Call Log Provider. The method ContentResolver.query() returns a Cursor. We iterate through the Cursor using the method moveToNext() to retrieve a list of phone number, duration of the call in seconds and call type (1 - Incoming, 2 - Outgoing, 3 - Missed).

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.CallLog
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity()
{
    private val permission: String = Manifest.permission.READ_CALL_LOG
    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 {
            readCallLog()
        }
    }

    private fun readCallLog()
    {
        val numberCol = CallLog.Calls.NUMBER
        val durationCol = CallLog.Calls.DURATION
        val typeCol = CallLog.Calls.TYPE // 1 - Incoming, 2 - Outgoing, 3 - Missed

        val projection = arrayOf(numberCol, durationCol, typeCol)

        val cursor = contentResolver.query(
            CallLog.Calls.CONTENT_URI,
            projection, null, null, null
        )

        val numberColIdx = cursor!!.getColumnIndex(numberCol)
        val durationColIdx = cursor.getColumnIndex(durationCol)
        val typeColIdx = cursor.getColumnIndex(typeCol)

        while (cursor.moveToNext()) {
            val number = cursor.getString(numberColIdx)
            val duration = cursor.getString(durationColIdx)
            val type = cursor.getString(typeColIdx)

            Log.d("MY_APP", "$number $duration $type")
        }

        cursor.close()
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.