【问题标题】:Getting multiple phone numbers from selected contact从选定的联系人获取多个电话号码
【发布时间】:2013-05-06 20:00:55
【问题描述】:

我正在开发一个应用程序,在该应用程序中我使用 startactivity 结果并在我的应用程序中访问默认的 android 电话簿,然后在选择一个联系人时,我得到了所选联系人的一个电话号码。我想检索多个电话号码,如果任何联系人有电话号码和电话号码类型,如工作、手机等。请帮助我,任何帮助将不胜感激。

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个,因为这对我有用:

    Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                startActivityForResult(intent, AppConstants.PICK_CONTACT);
    

    然后在 onActivityResult 中执行以下操作:

    Cursor cursor = null;
                String phoneNumber = "", primaryMobile = "";
    
                List<String> allNumbers = new ArrayList<String>();
                int contactIdColumnId = 0, phoneColumnID = 0, nameColumnID = 0;
                try {
                    Uri result = data.getData();
                    Utils.printLog(TAG, result.toString());
                    String id = result.getLastPathSegment();
                    cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
                    contactIdColumnId = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
                    phoneColumnID = cursor.getColumnIndex(Phone.DATA);
                    nameColumnID = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    
                    if (cursor.moveToFirst()) {
                        while (cursor.isAfterLast() == false) {
                            idContactBook = cursor.getString(contactIdColumnId);
                            displayName = cursor.getString(nameColumnID);
                            phoneNumber = cursor.getString(phoneColumnID);
    
                            if (phoneNumber.length() == 0)
                                continue;
    
                            int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
                            if (type == Phone.TYPE_MOBILE && primaryMobile.equals(""))
                                primaryMobile = phoneNumber;
                            allNumbers.add(phoneNumber);
                            cursor.moveToNext();
                        }
                    } else {
                        // no results actions
                    }
                } catch (Exception e) {
                    // error actions
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
    }
    

    【讨论】:

    • 通过给出这个Phone.TYPE_MOBILE,我们可以获得只有Mobile类型的电话号码,有什么方法可以让我获得与一个联系人相关的所有电话号码。感谢您的帮助
    • 为此,您需要添加 switch case 语句或 if else 例如: switch(type){ case Phone.TYPE_MOBILE: case Phone.TYPE_WORK: case Phone.TYPE_HOME: }
    • 你能给我举个例子吗?
    • 我在我之前的评论中给了你一个例子尝试在我的代码中使用它和 if else 条件,因为现在只有一个条件是 type_mobile 你可以添加一个 else if 语句与其他类型
    【解决方案2】:

    试试这个

    Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
    Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
    String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
    Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);
    

    【讨论】:

      猜你喜欢
      • 2015-05-18
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      相关资源
      最近更新 更多