I used start activity with intent for select contact and receive data from onActivityResult
method. onActivityResult
method is deprecated and have to use registerForActivityResult
solution.
Before:
val intent = Intent(Intent.ACTION_PICK).apply {
type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
}
startActivityForResult(intent, SELECT_PHONE_NUMBER)
if (requestCode == SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
val contactUri = data!!.data
val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER)
val cursor = context?.contentResolver?.query(
contactUri ?: Uri.EMPTY, projection,
null, null, null
);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
val numberIndex =
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
var number = cursor.getString(numberIndex) // ->> this is phone Number String
}
cursor?.close()
}
When I use
registerForActivityResult(ActivityResultContracts.PickContact())
I can't access phone number, can't query on DataProvider with
ContactsContract.CommonDataKinds.Phone.NUMBER
or
ContactsContract.PhoneLookup.NUMBER
I get an exception:
java.lang.IllegalArgumentException: Invalid column data1
or (when using CommonDataKinds.Phone.NUMBER)
java.lang.IllegalArgumentException: Invalid column number
(when using PhoneLookup.NUMBER)