从联系人簿中的联系人中获取单个电话号码,该联系人簿中保存了多个号码

时间:2015-03-16 06:40:51

标签: android android-contacts

我需要向用户询问联系电话以拨打电话。开按钮单击用户应直接重定向到通讯录,用户可以选择电话号码。以下是我现在使用的源代码。

Button buttonReadContact;
TextView textPhone;
final int RQS_PICKCONTACT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonReadContact = (Button)findViewById(R.id.readcontact);
    textPhone = (TextView)findViewById(R.id.phone);



    buttonReadContact.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            //Start activity to get contact
            final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
            Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
            startActivityForResult(intentPickContact, RQS_PICKCONTACT);
        }});

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

                }else{
                    textPhone.setText("NO Phone Number");
                }


            }else{
                Toast.makeText(getApplicationContext(), "NO data!", Toast.LENGTH_LONG).show();
            }
        }
    }
}

但现在问题是我只能从保存多个电话号码的联系人中选择一个号码。

我需要在Skype应用程序中执行此操作。当用户选择具有多个联系人的联系人时,从联系人簿本身,它应该要求用户选择该号码。请帮我做。

1 个答案:

答案 0 :(得分:2)

我使用此代码打开“联系人”并允许用户选择单个联系人,然后解析结果以显示联系人姓名,电话号码和缩略图照片。在下面的示例中,已定义成员mName,mPhoneNumber和mContactImage。

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                // Start an activity to pick a single contact (ACTION_PICK)
                Intent intent = new Intent(Intent.ACTION_PICK, 
                        ContactsContract.Contacts.CONTENT_URI);
                // Show only contacts with phone numbers
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                // Start the Contacts activity
                startActivityForResult(intent, PICK_CONTACT);
        }
    });

将结果解析为onActivityResult()。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case PICK_CONTACT :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI};
                Cursor c = getActivity().getContentResolver().query(contactData, projection, null, null, null);
                c.moveToFirst();
                int nameIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                int phoneNumberIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                int photoIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI);
                String name = c.getString(nameIdx);
                String phoneNumber = c.getString(phoneNumberIdx);
                String photo = c.getString(photoIdx);
                if (photo == null) {
                    // If no photo then substitute a dummy image
                    mContactImage.setImageResource(R.drawable.ic_contact_picture);
                } else {
                    // Display the contact photo
                    final Uri imageUri = Uri.parse(photo);
                    mContactImage.setImageURI(imageUri);
                }
                if (name == null) {
                    name = "No Name";
                }
                mName.setText(name);
                mPhoneNumber.setText(phoneNumber);
                c.close();

                // Now you have the phone number

            }
            break;
    }
}

我认为这可以回答你的问题。