如何在我的通讯录中获取对应姓名的电话号码?

Posted

技术标签:

【中文标题】如何在我的通讯录中获取对应姓名的电话号码?【英文标题】:how can I get phone numbers of corresponding names in my contacts? 【发布时间】:2016-07-29 01:32:01 【问题描述】:

我的光标查询中的这个选择子句只返回那些有电话号码的联系人,这就是我想要的:

// we only want contacts that have a name and a phone number. If they have a phone number, the value is 1 (if not, it is 0)
                ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1",

我的整个游标查询如下所示:

// this query only return contacts with phone number and is not duplicated
        phones = getContentResolver().query(
//                the table to query
                ContactsContract.Contacts.CONTENT_URI,
//                the columns to return
                null,
//               selection criteria :
// we only want contacts that have a name and a phone number. If they have a phone number, the value is 1 (if not, it is 0)
                ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1",
//               selection criteria
                null,
//                display in ascending order
                ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

但我怎样才能获得每个联系人的实际电话号码?我可以在上面的代码中添加一些东西,还是需要开始一个新的 Cursor 查询?

我认为是后者。

我开始了一个新的光标查询作为起点:

phonestwo = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                             null,
                             ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=1",
                             null,
                             null);

但是在 logcat 的日志中,我得到的电话光标有 134 条记录(正确,我想要的!),而我的电话光标有 196 条记录。简而言之,如何获取这 134 条记录对应的电话号码?

【问题讨论】:

【参考方案1】:

要获取与联系人关联的电话号码,您需要再次点击联系人内容提供程序。

首先向phonesCursor 询问联系人的id -

  String phoneContactId = phones.getString(phones.getColumnIndexOrThrow(BaseColumns._ID));

然后对于每个 phoneContactId,您获取其所有关联的电话号码 -

 Cursor pCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]  phoneContactId , null);

while (pCur.moveToNext()) 
                    int phoneType = pCur.getInt(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));
                    String phoneNumber = pCur
                            .getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

                

【讨论】:

【参考方案2】:
public class MainActivity extends Activity 


    Cursor cursor;
    ListView mainListView;
    ArrayList hashMapsArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (cursor != null) 
            cursor.moveToFirst();
        try 

            cursor = getApplicationContext().getContentResolver()
                    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            int Idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
            int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

            int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI);
            cursor.moveToFirst();


            Set<String> ids = new HashSet<>();
            do 
                System.out.println("=====>in while");
                String contactid=cursor.getString(Idx);
                if (!ids.contains(contactid)) 
                    ids.add(contactid);
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                   String  name = cursor.getString(nameIdx);
                    String phoneNumber = cursor.getString(phoneNumberIdx);
                    String image = cursor.getString(photoIdIdx);
                    System.out.println("Id--->"+contactid+"Name--->"+name);
                    System.out.println("Id--->"+contactid+"Name--->"+name);
                    System.out.println("Id--->"+contactid+"Number--->"+phoneNumber);

                    if (!phoneNumber.contains("*")) 
                        hashMap.put("contactid", "" + contactid);
                        hashMap.put("name", "" + name);
                        hashMap.put("phoneNumber", "" + phoneNumber);
                        hashMap.put("image", "" + image);
                        // hashMap.put("email", ""+email);
                        if (hashMapsArrayList != null) 
                            hashMapsArrayList.add(hashMap);
//                    hashMapsArrayList.add(hashMap);
                    
                

             while (cursor.moveToNext());


         catch (Exception e) 
            e.printStackTrace();
         finally 
            if (cursor != null) 
                cursor.close();
            
        


【讨论】:

以上是关于如何在我的通讯录中获取对应姓名的电话号码?的主要内容,如果未能解决你的问题,请参考以下文章

android:获取手机号码和姓名实现通讯录

如何仅使用他们的电话号码获取联系人姓名?

如何在应用程序中将联系人信息添加到通讯录中[重复]

Android编程,根据姓名获得手机通讯录中的电话号码

Android获取手机联系人的姓名和电话

如何从android中的联系人姓名获取联系人的号码