Thursday 17 February 2022

Android : Contact picker using registerForActivityResult

Adding sample to demonstrate registerForActivityResult use for accessing CONTACT from device contact list.

Previously we were able to pick contact from device contact list using startActivityForResult, but startActivityForResult is deprecated now, instead google suggest to use registerForActivityResult. 

https://developer.android.com/training/basics/intents/result


We can open contact list and retrieve the URI of selected contact using below code.

private val resultLauncher =
    registerForActivityResult(ActivityResultContracts.PickContact()) { uri ->
        uri?.let {
            Log.d(TAG, "Selected contact URI: $uri")
            binding.contactResultTv.text = retrievePhoneNumber(uri)
        }
    }
resultLauncher.launch(null)

We can retrieve actual contact from URI using below function.

@SuppressLint("Range")
private fun retrievePhoneNumber(uri: Uri): String {
    var phoneNumber = String.empty()

    val phone: Cursor? = this.contentResolver?.query(uri, null, null, null, null)
    phone?.let {
        if (it.moveToFirst()) {

            val id = it.getString(it.getColumnIndex(ContactsContract.Contacts._ID))
            val hasNumber =
                (it.getInt(it.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))

            if (hasNumber > 0) {
                val phoneCursor: Cursor? = this.contentResolver?.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", arrayOf(id),
                    null
                )
                phoneCursor?.let {
                    if (phoneCursor.moveToNext()) {
                        phoneNumber = phoneCursor.getString(
                            phoneCursor.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER
                            )
                        )
                    }
                    phoneCursor.close()
                }
            }
        }
        it.close()
    }
    return phoneNumber
}



For more detail please download App source code from below URL:

https://github.com/dipenptl1/contact-picker-kotlin



1 comment: