Select multiple contacts from phone book in androi

2019-02-01 22:10发布

I need to select contact numbers/emails from phone book in android.

I have seen selecting one contact and getting the result back in onActivityResult from this link.

But I need multiple contacts to be selected from the phone book. How to achieve this?

I don't want to make my custom list, is there a way to use androids built in functionality?

2条回答
放我归山
2楼-- · 2019-02-01 22:23

Looks like there's no such 'official' way. Your question is basically the same as this one How to select multiple contacts with Android SDK which has reference to custom implementation.

If You check AOSP Contacts application, there's no such possibility neither in documentation. The only thing I've observed from the source of AOSP Contacts is the following mention (in com.android.contacts.activities.PeopleActivity):

// TODO fix or remove multipicker code
//                else if (resultCode == RESULT_CANCELED && mMode == MODE_PICK_MULTIPLE_PHONES) {

From MMS app source code You can observe the following (in ComposeMessageActivity):

private void launchMultiplePhonePicker() {
    Intent intent = new Intent(Intents.ACTION_GET_MULTIPLE_PHONES);
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setType(Phone.CONTENT_TYPE);
    // We have to wait for the constructing complete.
    ContactList contacts = mRecipientsEditor.constructContactsFromInput(true);
    int urisCount = 0;
    Uri[] uris = new Uri[contacts.size()];
    urisCount = 0;
    for (Contact contact : contacts) {
        if (Contact.CONTACT_METHOD_TYPE_PHONE == contact.getContactMethodType()) {
                uris[urisCount++] = contact.getPhoneUri();
        }
    }
    if (urisCount > 0) {
        intent.putExtra(Intents.EXTRA_PHONE_URIS, uris);
    }
    startActivityForResult(intent, REQUEST_CODE_PICK);
}

Field Intents.ACTION_GET_MULTIPLE_PHONES is available in ContactsContract.java but I wasn't able to find any usage of it across AOSP. So, I think it's some dead code or that action get handled in some closed code. I've tried the same way to launch contacts selection from my test application and got only exception about no application to handle the action.

Of course, vendors Contacts applications provide such abilities (e.g. then selecting messages recipient), but it's better not to rely on them.

查看更多
老娘就宠你
3楼-- · 2019-02-01 22:43

I am sharing code for select multiple contact from Phone Book. you have change little bit and you can achieve your goal. thanks

 getAllContacts(this.getContentResolver());
    ListView lv = (ListView) findViewById(R.id.lv);
    ma = new MyAdapter();
    lv.setAdapter(ma);
    lv.setOnItemClickListener(this);
    lv.setItemsCanFocus(false);
    lv.setTextFilterEnabled(true);
    // adding
    select = (Button) findViewById(R.id.button1);
    select.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            StringBuilder checkedcontacts = new StringBuilder();

            for (int i = 0; i < name1.size(); i++)

            {
                if (ma.mCheckStates.get(i) == true) {
                    checkedcontacts.append(name1.get(i).toString());
                    checkedcontacts.append("\n");

                } else {

                }

            }

            Toast.makeText(Display.this, checkedcontacts, 1000).show();
        }
    });

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    ma.toggle(arg2);
}

public void getAllContacts(ContentResolver cr) {

    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        name1.add(name);
        phno1.add(phoneNumber);
    }

    phones.close();
}

class MyAdapter extends BaseAdapter implements
        CompoundButton.OnCheckedChangeListener {
    private SparseBooleanArray mCheckStates;
    LayoutInflater mInflater;
    TextView tv1, tv;
    CheckBox cb;

    MyAdapter() {
        mCheckStates = new SparseBooleanArray(name1.size());
        mInflater = (LayoutInflater) Display.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return name1.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (convertView == null)
            vi = mInflater.inflate(R.layout.row, null);
        tv = (TextView) vi.findViewById(R.id.textView1);
        tv1 = (TextView) vi.findViewById(R.id.textView2);
        cb = (CheckBox) vi.findViewById(R.id.checkBox1);
        tv.setText("Name :" + name1.get(position));
        tv1.setText("Phone No :" + phno1.get(position));
        cb.setTag(position);
        cb.setChecked(mCheckStates.get(position, false));
        cb.setOnCheckedChangeListener(this);

        return vi;
    }

    public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);
    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub

        mCheckStates.put((Integer) buttonView.getTag(), isChecked);
    }
查看更多
登录 后发表回答