使用联系人选择器时,从具有多个号码的用户中选择一个号码

时间:2011-05-30 00:25:47

标签: android android-contacts phone-number picker contactscontract

我正在尝试允许用户使用联系人选择器从联系人中选择电话号码。但是,现在我在网上看到的所有示例都显示了如何选择一个联系人,但我希望有一个第二个屏幕然后弹出,如果该联系人有多个电话号码,那么你可以指定你想选择哪一个(方式)当您选择联系人时,该短信允许您这样做。

我的问题是,您是否必须收集所有数字,然后要求用户选择一个号码,或者此功能是否已内置到Android中?我希望我忘了旗帜或其他东西。

4 个答案:

答案 0 :(得分:11)

或者,您最初可以在联系人选择器中显示与每个联系人关联的电话号码,然后选择一个。以这种方式启动联系人选择器(请注意与我的其他答案不同的URI):

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

然后,在onActivityResult():

Uri result = data.getData();
Log.v(TAG, "Got a result: " + result.toString());

// get the phone number id from the Uri
String id = result.getLastPathSegment();

// query the phone numbers for the selected phone number id
Cursor c = getContentResolver().query(
    Phone.CONTENT_URI, null,
    Phone._ID + "=?",
    new String[]{id}, null);

int phoneIdx = c.getColumnIndex(Phone.NUMBER);

if(c.getCount() == 1) { // contact has a single phone number
    // get the only phone number
    if(c.moveToFirst()) {
        phone = c.getString(phoneIdx);
        Log.v(TAG, "Got phone number: " + phone);

        loadContactInfo(phone); // do something with the phone number

    } else {
        Log.w(TAG, "No results");
    }
}

答案 1 :(得分:5)

我可以通过创建第二个对话框来显示与联系人关联的所有电话号码。 首先,在代码中的某处调用它:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_PICK_CONTACT);

然后在onActivityResult()中使用此选项来决定所选联系人是否有多个电话号码,如果是,则显示一个对话框:

Uri result = data.getData();
Log.v(TAG, "Got a result: " + result.toString());

// get the contact id from the Uri
String id = result.getLastPathSegment();

// query for phone numbers for the selected contact id
c = getContentResolver().query(
    Phone.CONTENT_URI, null,
    Phone.CONTACT_ID + "=?",
    new String[]{id}, null);

int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);

if(c.getCount() > 1) { // contact has multiple phone numbers
    final CharSequence[] numbers = new CharSequence[c.getCount()];
    int i=0;
    if(c.moveToFirst()) {
        while(!c.isAfterLast()) { // for each phone number, add it to the numbers array
            String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number
            String number = type + ": " + c.getString(phoneIdx);
            numbers[i++] = number;
            c.moveToNext();
        }
        // build and show a simple dialog that allows the user to select a number
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.select_contact_phone_number_and_type);
        builder.setItems(numbers, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int item) {
                String number = (String) numbers[item];
                int index = number.indexOf(":");
                number = number.substring(index + 2);
                loadContactInfo(number); // do something with the selected number
            }
        });
        AlertDialog alert = builder.create();
        alert.setOwnerActivity(this);
        alert.show();

    } else Log.w(TAG, "No results");
} else if(c.getCount() == 1) {
    // contact has a single phone number, so there's no need to display a second dialog
}

我知道这是一个老问题,但我希望它有所帮助。

答案 2 :(得分:3)

万一有人偶然发现了这一点。

其他答案的另一种选择是库https://github.com/codinguser/android_contact_picker

完全披露:我是此图书馆的作者

答案 3 :(得分:0)

在Android开发者参考上简单解释: https://developer.android.com/training/contacts-provider/modify-data.html#InsertEdit

和简单的附加代码:

String phoneNumber = "+01 123 456 789";
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber);
if (intent.resolveActivity(getPackageManager()) != null) {
 startActivityForResult(intent, REQUEST_CODE_ADD_PHONE_CONTACT);
}

如果您需要活动结果,则必须通过REQUEST_CODE_ADD_PHONE_CONTACT变量监听Activity上的onActivityResult事件。​​