Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

android - All phone numbers of one contact for startActivityForResult

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?

Edit: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...