android - 获取与一个联系人android关联的所有手机号码

标签 android contactscontract

我已将联系人添加到我的通讯录中,其中有多个号码,如下所示。

我想使用 ContactsContract 内容 uri 获取所有 3 个“用户”。 通过使用下面的代码,我只有一个联系人。

 Cursor cursorAddressBook = mContentResolver.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    if (cursorAddressBook != null) {
        while (cursorAddressBook.moveToNext()) {

            String dataName = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String dataNumber = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            int dataType = cursorAddressBook.getInt(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA2));
            String contactId = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

            Log.e("last updat Name last", dataName);
            Log.e("last updated No last", dataNumber);
            Log.e("last updated Type last", dataType);


        }
        cursorAddressBook.close();
    }

最佳答案

从这里blogpost如果一个联系人有多个电话号码,那么您可以在 Android 中使用 Android 的内置类(Cursor 和 ContactsContract)检索所有电话号码和其他详细信息。您需要根据电话类型检索联系电话,例如(TYPE_MOBILE、TYPE_HOME 等)

{
    Cursor cursor = cntx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list 
    if (contactsCount > 0)
    {
        while(cursor.moveToNext())
        {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
            {
                //the below cursor will give you details for multiple contacts
                Cursor pCursor = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                                                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                                                new String[]{id}, null);
                // continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list  
                while (pCursor.moveToNext())
                 {
                      int phoneType         = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                      //String isStarred        = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
                      String phoneNo    = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      //you will get all phone numbers according to it's type as below switch case.
                      //Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want.
                      switch (phoneType)
                      {
                            case Phone.TYPE_MOBILE:
                                Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo);
                                break;
                            case Phone.TYPE_HOME:
                                Log.e(contactName + ": TYPE_HOME", " " + phoneNo);
                                break;
                            case Phone.TYPE_WORK:
                                Log.e(contactName + ": TYPE_WORK", " " + phoneNo);
                                break;
                            case Phone.TYPE_WORK_MOBILE:
                                Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo);
                                break;            
                            case Phone.TYPE_OTHER:
                                Log.e(contactName + ": TYPE_OTHER", " " + phoneNo);
                                break;
                            default:
                                break;
                      }
              }
              pCursor.close();
            }
        }
        cursor.close();
    }
}

关于android - 获取与一个联系人android关联的所有手机号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46688535/

相关文章:

android - 如何在android中以天,小时(24),分钟(60),秒(60)获取两个日期之间的差异

android - UDP 数据包似乎被 android 4.1 阻止

android - 更新自己的个人资料图片 - 无法将 BLOB 转换为字符串

Android - 在电话上接收联系人更改

android - 使用电子邮件 ID 获取联系人

Android 如何检测联系人列表已更改?

安卓支持工具栏 : Resizing won't realign menu items

Android 操作系统框架 - 将单个可执行文件构建为共享对象

未找到 androidx.lifecycle.DefaultLifecycleObserver

安卓通讯录