This was a headache… I just noticed that we have to check for permission in the code, not just adding it into the Manifest, and I think every professional coder should do that… but anyway i didn’t include it in my code…
for this intent to be done (Dialing the number) i did as follows…
I created a button and applied this to it
/**
* Getting contact Phone URI to fetch the number
*/
callSuspect.apply {
val pickPhoneIntent = Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI)
setOnClickListener {
startActivityForResult(pickPhoneIntent, REQUEST_PHONE)
}
}
then in onActivityResult i checked for REQUEST_PHONE
/**
* Get the Phone Number
*/
requestCode == REQUEST_PHONE && data != null -> {
val contactURI : Uri? = data.data
//Got the phone ID
val queryFields = ContactsContract.CommonDataKinds.Phone._ID
//Perform Your Query - the Phone.CONTENT_URI is like a "where" clause here
val cursor =
requireActivity().contentResolver
.query(contactURI!!, null, queryFields, null, null)
cursor.use {
if (it?.count == 0) {
return
}
it?.moveToFirst()
val number = it?.getString(it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
val dialNumber = Intent(Intent.ACTION_DIAL)
dialNumber.data = Uri.parse("tel: $number")
startActivity(dialNumber)
}
cursor?.close()
}
Happy Dialing…