之一接触startActivityForResult所有电话号码电话号码、startActivityForResult

2023-09-04 13:25:56 作者:醉魂愁梦相伴

一个联系人可以有多个电话号码(移动,家庭,..)。我想使用户能够选择一个特定的联系人的电话号码之一。

A contact may have many phone numbers (mobile, home, ..). I want to enable the user to pick one of the phone numbers of a specific contact.

在这个片段中,我得到的所有电话号码的列表中为每个联系人。

With this snippet I get the list of all phone numbers for each contact.

Intent intent = new Intent(Intent.ACTION_PICK, 
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PHONE_NUMBER_PICKED);

我怎么能列出一个接触只有电话号码?

How can I list only the phone numbers of one contact?

编辑:我知道如何获取联系人的所有电话号码,这不是问题的关键。我可以把所有的电话号码列表视图,并得到用户选择一个。但是,这个功能的存在(上面提到的),我只是不希望所有的数字,但只有电话号码一个接触。

I know how to get all phone numbers of a contact, that's not the point. I could put all phone numbers in a listview and get the user to pick one. But this functionality exists (mentioned above), I just don't want all numbers, but only the phone numbers for one contact.

推荐答案

如果你想获得所有相关的联系人,然后电话号码:

IF you want to get all the phone numbers related to a contact then:

1)使用此意图打开联系人应用程序:

1) Use this intent to open contacts app:

 Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    startActivityForResult(intent, PICK_CONTACT);

2) onActivityResult 使用以下code:

2) in onActivityResult use following code:

if (requestCode == PICK_CONTACT) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    Uri contactData = data.getData();

                    try {

                        String id = contactData.getLastPathSegment();
                        Cursor phoneCur = getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[] { id },
                                        null);

                        final ArrayList<String> phonesList = new ArrayList<String>();
                        while (phoneCur.moveToNext()) {
                            // This would allow you get several phone addresses
                            // if the phone addresses were stored in an array
                            String phone = phoneCur
                                    .getString(phoneCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                            phonesList.add(phone);
                        }
                        phoneCur.close();

                        if (phonesList.size() == 0) {
                            Helper.showToast(
                                    this,
                                    getString(R.string.error_no_phone_no_in_contact),
                                    Toast.LENGTH_LONG);
                        } else if (phonesList.size() == 1) {
                            editText.setText(phonesList.get(0));
                        } else {

                            final String[] phonesArr = new String[phonesList
                                    .size()];
                            for (int i = 0; i < phonesList.size(); i++) {
                                phonesArr[i] = phonesList.get(i);
                            }

                            AlertDialog.Builder dialog = new AlertDialog.Builder(
                                    SendSMS.this);
                            dialog.setTitle(R.string.choose_phone);
                            ((Builder) dialog).setItems(phonesArr,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            String selectedEmail = phonesArr[which];
                                            editText.setText(selectedEmail);
                                        }
                                    }).create();
                            dialog.show();
                        }
                    } catch (Exception e) {
                        Log.e("FILES", "Failed to get phone data", e);
                    }
                }
            }
        }

这将在名为EDITTEXT一个编辑文本设置选择的电话号码。您可以更改此根据您的需要。

This will set the selected phone no in a edit text named editText. You can change this as per your need.