Read Contacts in Android

Android allows to read contacts using Contacts Provider. It manages access to a central repository that stores the contact details of the people. The Contacts Provider is a content provider component.

Application should have access to read contacts. So we need to request the READ_CONTACTS 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_CONTACTS" />

    <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 Contacts Provider. The method ContentResolver.query() returns a Cursor. We iterate through the Cursor using the method moveToNext() to retrieve a list of contact ID, name and phone number.

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

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

    private fun readContacts()
    {
        val contactIdCol = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
        val nameCol =  ContactsContract.Contacts.DISPLAY_NAME
        val numberCol = ContactsContract.CommonDataKinds.Phone.NUMBER

        val projection = arrayOf(contactIdCol, nameCol, numberCol)

        val cursor = contentResolver.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            projection, null, null, null
        )

        val contactIdIdx = cursor!!.getColumnIndex(contactIdCol)
        val nameIdx = cursor.getColumnIndex(nameCol)
        val numberIdx = cursor.getColumnIndex(numberCol)

        while (cursor.moveToNext()) {
            val contactId = cursor.getString(contactIdIdx)
            val name = cursor.getString(nameIdx)
            val number = cursor.getString(numberIdx)

            Log.d("MY_APP", "$contactId $name $number")
        }

        cursor.close()
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *