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

发布于 2024-11-10 12:46:56 字数 213 浏览 7 评论 0原文

我试图允许用户使用联系人选择器从联系人中选择电话号码。但是,现在我在网上看到的所有示例都显示了如何选择联系人,但我希望如果该联系人有多个电话号码,那么会弹出第二个屏幕,以便您可以指定要选择哪个电话号码(方式当您选择联系人时,该短信可让您执行此操作)。

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

I'm trying to allow a user to select a phone number from a contact using the contact picker. However, right now all the examples I see online show how you can select a contact, but I am hoping to have a second screen then pop up if that contact has multiple phone numbers so you can specify which one you want to select (the way that text message lets you do so when you select a contact).

My question is, do you have to gather all of the numbers and then ask the user to select a number, or is this functionality already built into Android? I'm hoping I just forgot a flag or something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聚集的泪 2024-11-17 12:46:57

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

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
}

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

I was able to do this by creating a second dialog which shows all the phone numbers associated with a contact.
First, call this somewhere in your code:

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

Then in onActivityResult() use this to decide if the selected contact has multiple phone numbers, and display a dialog if so:

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
}

I know this is an old question but I hope it helps.

狼性发作 2024-11-17 12:46:57

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

其他答案的另一个替代方案是库 https://github.com/codinguser/android_contact_picker

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

Just in case someone stumbles across this again.

Another alternative to the other answers is the library https://github.com/codinguser/android_contact_picker

Full Disclosure: I am the author of this library

烏雲後面有陽光 2024-11-17 12:46:57

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);
}

如果您需要活动结果,您必须通过以下方式监听活动上的 onActivityResult 事件REQUEST_CODE_ADD_PHONE_CONTACT 变量。

it is simple explained on android developers reference:
https://developer.android.com/training/contacts-provider/modify-data.html#InsertEdit

and simple append code:

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);
}

if you needed activity result, you must listen to onActivityResult event on Activity by REQUEST_CODE_ADD_PHONE_CONTACT variable.

甜妞爱困 2024-11-17 12:46:56

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

Alternatively, you can initially display the phone numbers associated with each contact in the Contact Picker and select one that way. Launch contact picker this way (note the different URI than my other answer):

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

Then, in 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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文