3

我正在尝试从联系人中获取第二个号码。现在我可以获取第一个数字。我想获取特定联系人的第二个或第三个号码。这可能吗?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(resultCode == RESULT_OK){
        if(requestCode == RQS_PICKCONTACT){
            Uri returnUri = data.getData();
            Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);

            if(cursor.moveToNext()){
                int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                String contactID = cursor.getString(columnIndex_ID);

                int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);

                if(stringHasPhoneNumber.equalsIgnoreCase("1")){
                    Cursor cursorNum = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID, 
                            null, 
                            null);

                    //Get the first phone number
                    if(cursorNum.moveToNext()){
                        int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        String stringNumber = cursorNum.getString(columnIndex_number);
                        textPhone.setText(stringNumber);
                    }

让我知道!

谢谢!

4

1 回答 1

2

您当前的代码将仅提供第一个电话号码,要获取所有电话号码,只需遍历光标,它将为您提供给定联系人的所有子电话号码

while (cursorNum.moveToNext()) {
    int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    String stringNumber = cursorNum.getString(columnIndex_number);
    Log.i(TAG,"Contact number "+stringNumber);

    // do your desired task here. 

}
于 2013-01-24T11:12:10.860 回答