从Android中的联系人中获取第二个号码

时间:2013-01-24 10:59:42

标签: android android-contacts

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

@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);
                    }

让我知道!

谢谢!

1 个答案:

答案 0 :(得分:2)

您当前的代码只会给出第一个电话号码,要让所有电话号码只是通过光标迭代,它会为您提供给定联系人的所有子电话号码。< / p>

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. 

}