【问题标题】:Selecting a number from user with multiple numbers when using the contact picker使用联系人选择器时从具有多个号码的用户中选择一个号码
【发布时间】:2011-09-04 12:09:41
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 另外,如果您添加一些代码,您也可以获得显示名称:int nameIdx = c.getColumnIndex(Phone.DISPLAY_NAME); //insert this below where phoneIdx is createdcontactName = c.getString(nameIdx); //insert this below phone = c.getString(phoneIdx);
    • @howettl 是否有任何 URI 传递会显示单个对话框(在联系人应用程序中,如上述 URI 所做的那样)来选择电子邮件以及电话号码?
    【解决方案2】:

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

    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
    }
    

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

    【讨论】:

    • 我怎样才能得到名字??
    • c.getCount() == 1时如何获取号码。我试过contactNumber = c.getColumnIndex(phoneIdx);但它给了我 android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 this error
    【解决方案3】:

    以防万一有人再次遇到这种情况。

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

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

    【讨论】:

    • 我必须找到一种方法来扩展您的库以选择电子邮件(n 中的 1 个)!!
    • 我觉得应该比较容易做到
    • 确实没那么容易。无论如何,我不会再在这个库上浪费时间了,因为它根本不适合我(我正在使用 Eclipse)。所以,我最好找到其他解决方案。
    • 没错,最后我检查了 Eclipse 对 Maven 中 Android 库项目的支持,还有很多不足之处。因此,对于 Eclipse,您只需从 Maven 中心 search.maven.org/remotecontent?filepath=com/codinguser/android/… 下载 jar,然后将其包含在您的项目中,就像您将使用的任何其他库项目一样!
    【解决方案4】:

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

    如果你需要activity结果,你必须通过REQUEST_CODE_ADD_PHONE_CONTACT变量监听Activity上的onActivityResult事件。​​

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2013-02-07
      • 2014-04-04
      • 2012-05-14
      • 2011-09-03
      • 2017-10-06
      • 2018-01-08
      • 2019-06-15
      相关资源
      最近更新 更多