美文网首页
Android 选择指定联系人数据

Android 选择指定联系人数据

作者: _Sisyphus | 来源:发表于2017-11-13 16:13 被阅读0次

    不需要权限,拿来即用哈~

    public static final int REQUEST_SELECT_PHONE_NUMBER = 1;
    
    public void selectContact() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
    
            Uri contactUri = data.getData();
            String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
            Cursor cursor = getContentResolver().query(contactUri, projection,
                    null, null, null);
            
            if (cursor != null && cursor.moveToFirst()) {
                int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    
                String name = cursor.getString(nameIndex);      //联系人姓名
                String number = cursor.getString(numberIndex);  //联系人号码
    
                Log.e(TAG, "onActivityResult: " + name);
                Log.e(TAG, "onActivityResult: " + number);
    
                cursor.close();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Android 选择指定联系人数据

          本文链接:https://www.haomeiwen.com/subject/crmzmxtx.html