Android多个联系人选择显示名称

时间:2015-08-20 16:46:30

标签: android

虽然下面的代码给了我多个联系人,但我也希望得到联系人姓名。

public void pickContact() {
    try {
        Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
        phonebookIntent.putExtra("additional", "phone-multi");
        startActivityForResult(phonebookIntent, 1);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void onActivityResult(int reqCode, int resultCode, Intent data) {

    if (reqCode != 1) {
        Log.d("Val1", reqCode + "");
        return;
    }

    if(resultCode != Activity.RESULT_OK) {    
        Log.d("Val2", resultCode + ":" + Activity.RESULT_OK);
        return;
    }

    Bundle contactUri = data.getExtras();
    if (null == contactUri) {
        Log.d("Val1", reqCode + "null");
        return;
    }

    ArrayList<String> contacts = (ArrayList<String>)contactUri.get("result");
    Log.d("values", contacts.get(0) + ":" + contacts.size());
}

1 个答案:

答案 0 :(得分:3)

如果还有人没有得到答案,我就得到了一个..如果您使用下面的代码,您将获得所有联系人和姓名,在这里您必须编写自己的适配器来显示它们,让用户选择多个联系人。谢谢:))

public static ArrayList<Contact> getContactsList(Context context) {
    ArrayList<Contact> contacts=new ArrayList<>();
    Cursor phones = context.getContentResolver().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));
        contacts.add(new Contact(name,phoneNumber));
    }
    phones.close();
    return contacts;
}