从联系人 Android 获取单个电话号码

Nas*_*raz 5 android phone-number android-contacts

嗨,我正在尝试从联系人列表中获取一个电话号码。我找到了可以让我获得整个联系人列表电话号码的代码,我想要的只是所点击项目的电话号码。任何帮助将不胜感激。谢谢 ..

public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);

ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",new String[] { id }, null);

                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(getActivity(), phoneNo,Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
});
Run Code Online (Sandbox Code Playgroud)

joa*_*t4u 3

当您在联系人列表中单击联系人时,您将获得您的Intent.ACTION_PICK意图的结果。

因此,要处理结果,您必须重写onActivityResult()方法。

Intent您会在返回的数据对象中收到联系人的查找 URI onActivityResult()

所以,你所要做的就是:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == CONTACT_PICKER_RESULT){
        if(resultCode==Activity.RESULT_OK){

            Uri uri = data.getData();
            ContentResolver contentResolver = getContentResolver();
            Cursor contentCursor = contentResolver.query(uri, null, null,null, null);

            if(contentCursor.moveToFirst()){
                String id = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                String hasPhone =
                        contentCursor.getString(contentCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if (hasPhone.equalsIgnoreCase("1")) 
                {
                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                    phones.moveToFirst();
                    String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i("phoneNUmber", "The phone number is "+ contactNumber);

                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你只需要用 的值做任何你想做的事contactNumber

希望对您有帮助。