1.在选择联系人方式网上也有很多案例 有的说是使用ContactsContract.CommonDataKinds.Phone.CONTENT_URI也有的说是使用ContactsContract.Contacts.CONTENT_URI其实这两种方式都可以使用 只不过ContactsContract.Contacts.CONTENT_URI这种方式需要多查询一遍
一、使用ContactsContract.CommonDataKinds.Phone.CONTENT_URI跳转到选择联系人
//跳转到选择联系人界面
private fun openLinkman() {
val intent = Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI)
linkmanResultLauncher.launch(intent)
}
//联系人回调
private val linkmanResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val contactUri: Uri = result.data?.data ?: return@registerForActivityResult
val projection: Array<String> = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER)
contentResolver.query(contactUri, projection, null, null, null).use { cursor ->
if (cursor != null && cursor.moveToFirst()) {
val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
val displayName = cursor.getString(nameIndex)
val phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
val phoneNumber = cursor.getString(phoneIndex)
Log.e(TAG, "姓名:$displayName 手机号:$phoneNumber count:${cursor.count}")
}
}
}
}
这里把openLinkman方法改成这样也是一样的
private fun openLinkman() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
linkmanResultLauncher.launch(intent)
}
展示效果
二、使用ContactsContract.Contacts.CONTENT_URI跳转到选择联系人
private fun openLinkman() {
val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
linkmanResultLauncher2.launch(intent)
}
//联系人回调
private val linkmanResultLauncher2 = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val contactUri: Uri = result.data?.data ?: return@registerForActivityResult
contentResolver.query(contactUri, null, null, null, null)?.use {cursor->
if (cursor.moveToNext()){
val nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
//获取联系人姓名
val displayName = cursor.getString(nameIndex)
var phoneNumber = ""
//获取id
val idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID)
val id = cursor.getString(idIndex)
//判断是否有手机号
val hasPhoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER)
val hasPhone = cursor.getString(hasPhoneIndex) //等于1就是有手机号
if(hasPhone == "1"){
//重新查询手机号
contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,"${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = $id",null,null)?.use { phonesCursor->
while (phonesCursor.moveToNext()){
val phoneIndex = phonesCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
phoneNumber = phonesCursor.getString(phoneIndex)
}
}
}
Log.e(TAG, "姓名:$displayName 手机号:$phoneNumber")
}
}
}
}
展示效果
两则的区别
1. 第一种联系人和姓名展示出来了
2. 第二种只展示一个姓名 手机号又重新查询返回的 这种一个人又多个手机号的不能单独选择代码中是当前联系人的最后一个号码
这两种方式看需求使用就好了