无法获取联系人姓名

Posted

技术标签:

【中文标题】无法获取联系人姓名【英文标题】:Unable to fetch contact names 【发布时间】:2015-10-12 07:54:28 【问题描述】:

我正在尝试使用此代码检索联系人号码和联系人姓名。我得到了数字,但名称列表返回 null

ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if(cursor.moveToFirst())
    
        List<String> ContactNames = new ArrayList<String>();
        do
        
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[] id , null);

                   while (pCur.moveToNext())
                
                    String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String contactNames = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    ContactNames.add(contactNames);
                    break;
                
                pCur.close();
            

         while (cursor.moveToNext()) ;
    

我不知道我错过了什么。请帮忙

【问题讨论】:

"字符串 contactNames = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));"你需要 CONTACT_ID 做什么? 检查这个所以问题它有你需要的一切***.com/questions/6152442/how-to-get-contact-email-id @KaranMer 这将非常慢,如果与数百个联系人一起使用可能需要一分钟或更长时间 @pskink 抱歉我更新了它.. 实际上我正在尝试它是否返回任何内容 @KaranMer 和这个比较一下:***.com/a/26820544/2252830 【参考方案1】:

电话读取联系人列表:

import java.util.ArrayList;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;

public class ContactsUtils 

    public static ArrayList<ContactModel> getContactList(Context ctx) 

        ArrayList<ContactModel> list = new ArrayList<ContactModel>();
        String displayName = "", phoneNo = "", email = " ";
        ContentResolver contentResolver = ctx.getContentResolver();
        Cursor curMain = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (curMain.getCount() > 0) 
            while (curMain.moveToNext()) 
                String contactId = curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts._ID));
                displayName = curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                     Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]contactId, null);
                     contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[]  contactId , null);
                     Cursor emailCursor = contentResolver.query(Email.CONTENT_URI, null,Email.CONTACT_ID + " = " + contactId, null, null);
                        if(emailCursor.getCount()>0)
                              while (emailCursor.moveToNext()) 
                                  email =   emailCursor.getString(emailCursor.getColumnIndex(Email.DATA));
                            
                        else
                            email = "";
                        
                        emailCursor.close();
                        if(phoneCursor.getCount()>0)
                              while (phoneCursor.moveToNext()) 
                                  phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                              
                        else
                            phoneNo = "";
                        
                      phoneCursor.close();

                    ContactModel cm = new ContactModel();
                    cm.setDisplayName(displayName);
                    cm.setPhoneNumber(phoneNo);
                    cm.setEmail(email);
                    list.add(cm);
                
            
        
         curMain.close();
        return list;
    

    public static class ContactModel implements Comparable<ContactModel>
        private String displayName = "", phoneNumber = "", email = "";



        @Override
        public int compareTo(ContactModel another) 
            return getDisplayName().compareTo(another.getDisplayName());    
        


        public String getEmail() 
            return email;
        

        public void setEmail(String email) 
            this.email = email;
        

        public String getDisplayName() 
            return displayName;
        

        public void setDisplayName(String displayName) 
            this.displayName = displayName;
        

        public String getPhoneNumber() 
            return phoneNumber;
        

        public void setPhoneNumber(String phoneNumber) 
            this.phoneNumber = phoneNumber;
        


        public ContactModel() 
        

        public ContactModel(String name) 
            this.displayName = name;
        

        public String toString() 
            return displayName;
        
    

Manifest文件中添加权限

<uses-permission android:name="android.permission.READ_CONTACTS"/>

希望对你有帮助。

【讨论】:

这将非常慢,如果与数百个联系人一起使用可能需要一分钟或更长时间 @pskink,感谢分享关心,我们可以使用 AsyncTask 任务吗? AsyncTask 无济于事:基本上算法很慢,对比一下这个:***.com/a/26820544/2252830

以上是关于无法获取联系人姓名的主要内容,如果未能解决你的问题,请参考以下文章

获取联系人姓名?

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

android获取联系人图片、姓名和手机号码

从联系 Swift 获取街道地址

从android中的指定组获取联系人

如何从联系人列表中获取姓名