Android中两种选择联系人方式

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. 第二种只展示一个姓名  手机号又重新查询返回的 这种一个人又多个手机号的不能单独选\n择代码中是当前联系人的最后一个号码(需要申请android.permission.READ_CONTACTS权限)

这两种方式看需求使用就好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值