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

12

我想让用户使用联系人选择器从联系人中选择一个电话号码。 然而,目前我在网上看到的所有示例都展示了如何选择联系人,但是我希望如果该联系人有多个电话号码,则会弹出第二个屏幕,以便您可以指定要选择哪个号码(就像在选择联系人时文本消息让您这样做)。

我的问题是,您是否必须收集所有号码,然后要求用户选择一个号码,还是Android已经内置了此功能? 我希望我只是忘记了某个标志或类似的东西。

4个回答

12

或者,您可以最初在“联系人选择器”中显示与每个联系人关联的电话号码,并通过此方式选择一个联系人。以这种方式启动联系人选择器(注意与我的其他答案不同的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");
    }
}

2
此外,如果您添加一些代码,您也可以获取显示名称:int nameIdx = c.getColumnIndex(Phone.DISPLAY_NAME); //在创建phoneIdx下面插入此内容 以及 contactName = c.getString(nameIdx); //在 phone = c.getString(phoneIdx); 下面插入此内容 - mnearents
@howettl 有没有可以传递 URI 的方法,可以显示一个单独的对话框(与上述 URI 相同的联系人应用程序内),并选择电子邮件和电话号码? - cgr

6

我可以通过创建第二个对话框来显示与联系人相关联的所有电话号码来实现这一点。 首先,请在您的代码中的某个位置调用此函数:

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
}

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


我怎样才能获得姓名呢? - dmSherazi
如何在 c.getCount() == 1 时获取数字。我尝试使用 contactNumber = c.getColumnIndex(phoneIdx); 但它会给我一个 android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 的错误。 - Hirak Chhatbar

3

我必须找到一种方法来扩展您的库,以便也可以选择电子邮件(1 / n)!! - Phantômaxx
我认为这应该相对容易实现。 - codinguser
确实不太容易。无论如何,我不会再浪费时间在这个库上了,因为它对我来说根本不起作用(我正在使用Eclipse)。所以,我最好找到其他解决方案。 - Phantômaxx
据我所知,Eclipse对Maven中的Android库项目支持还有很大的改进空间。因此,在Eclipse中,您可以从Maven中央仓库下载jar包http://search.maven.org/remotecontent?filepath=com/codinguser/android/contactpicker/2.2.0/contactpicker-2.2.0.jar,然后像使用其他库项目一样将其包含在您的项目中! - codinguser

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事件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接