android 选择特定联系人数据

这篇博客讲述了在Android中如何经过一天的资料查找和测试,成功实现读取特定联系人数据的方法。首先通过跳转到联系人列表,然后在onActivityResult方法中处理返回结果,最终在华为和小米设备上得到验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android读取特定联系人是真的烦, 网上代码一大堆,但是拿过来又不好使,无奈,只有自己一点一点弄了(找资料+测试 = 一天时间)。
首先需要跳转到联系人列表:

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, PEOPLE_REQUEST_CODE);
        }

接下来重新onActivityResult方法

if(resultCode == RESULT_OK){
            if(requestCode == PEOPLE_REQUEST_CODE){
                if(null == data){
                    return;
                }
                //获得选取的联系人信息
                Uri mUri = data.getData();
                String [] contact=getPhoneContacts(mUri);
                if(contact != null){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put(contact[0],contact[1]);
                    contacts.add(map);
                    adapter.notifyDataSetChanged();
                }

            }
        }
private String[] getPhoneContacts(Uri uri){
        String [] contacts = new String[2];
        String[] projectionNumber = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursorNumber = getContentResolver().query(uri, projectionNumber, null, null, null);
        Log.e(TAG, "Cursor: "+cursorNumber.getPosition() );
        // If the cursor returned is valid, get the phone number
        if (cursorNumber != null && cursorNumber.moveToFirst()) {
            int numberIndex = cursorNumber.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            Log.e(TAG, "getPhoneContacts: numberIndex"+"-->"+numberIndex );
            String number = cursorNumber.getString(numberIndex);
            // Do something with the phone number
            number = number.replaceAll("-","");
            number = number.replaceAll(" ","");
            Log.e(TAG, "getPhoneContacts: number"+"-->"+number );
            contacts[1] = number;
            cursorNumber.close();
        }else{
            cursorNumber.close();
            showMissingPermissionDialog();
            return null;
        }

        String[] projectionName = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
        Cursor cursorName = getContentResolver().query(uri, projectionName, null, null, null);
        if (cursorName != null && cursorName.moveToFirst()) {
            int nameIndex = cursorName.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            Log.e(TAG, "getPhoneContacts: nameIndex"+"-->"+nameIndex );
            String name = cursorName.getString(nameIndex);
            Log.e(TAG, "getPhoneContacts: name"+"-->"+name );
            // Do something with the phone name
            contacts[0] = name;
            cursorName.close();
        }else{
            cursorName.close();
            showMissingPermissionDialog();
            return null;
        }

        return contacts;
    }
/**
     * 显示提示信息
     *
     * @since 2.5.0
     */
    private void showMissingPermissionDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.notifyTitle);
        builder.setMessage(R.string.notifyContactMsg);

        // 拒绝, 退出应用
        builder.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        // 设置,管理权限
        builder.setPositiveButton(R.string.setting,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startAppSettings();
                    }
                });

        builder.setCancelable(false);

        builder.show();
    }

    /**
     * 启动应用的设置
     *
     * @since 2.5.0
     */
    private void startAppSettings() {
        Intent intent = new Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, SETTING_REQUESTCODE);
    }

这是我最后一点一点测试出来的结果,目前华为、小米测试可以获取
最后附上资料链接

https://developer.android.google.cn/guide/components/intents-common.html#Contacts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值