12

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

我的问题是,您是否必须收集所有数字然后要求用户选择一个数字,或者这个功能是否已经内置在 Android 中?我希望我只是忘记了一面旗帜之类的。

4

4 回答 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");
    }
}
于 2011-07-05T18:16:18.130 回答
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
}

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

于 2011-07-05T17:48:25.143 回答
3

以防万一有人再次偶然发现这一点。

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

完全披露:我是这个库的作者

于 2011-12-05T15:52:22.407 回答
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 事件。

于 2015-08-24T13:44:38.430 回答