Android 使用contacts Contact获取联系人的最佳方式

Android 使用contacts Contact获取联系人的最佳方式,android,contact,contactscontract,Android,Contact,Contactscontract,我正在尝试获取电话簿中可见的联系人。假设有5个联系人的号码保存在我的设备中,3个联系人的号码保存在我的Gmail帐户中。此外,考虑设备接触跨越Gmail接触。以下方法输出所有8个触点: public void getContactList() { contactList.clear(); Cursor phones = null; try { phones = getContentResolver().query(ContactsContract.Co

我正在尝试获取电话簿中可见的联系人。假设有5个联系人的号码保存在我的设备中,3个联系人的号码保存在我的Gmail帐户中。此外,考虑设备接触跨越Gmail接触。以下方法输出所有8个触点:

public void getContactList() {
    contactList.clear();

    Cursor phones = null;

    try {
        phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (phones.moveToNext())
        {
            String _name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String _number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            HashMap<String , String> contactMap= new HashMap<String, String>();

            contactMap.put("NAME", _name);
            contactMap.put("NUMBER", _number);

            contactList.add(contactMap);
            numbers.add("+" + _number);
            Log.v("" + counter++, _number + " " + _name);
        }
        phones.close();

    } catch ( Exception e ) {
        // TODO: handle exception
    }
    finally {
        if(phones != null){
            phones.close();
        }
    }
}
public void getContactList(){
contactList.clear();
光标=空;
试一试{
phones=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,null);
while(phones.moveToNext())
{
String _name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name));
String _number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.number));
HashMap contactMap=新建HashMap();
contactMap.put(“名称”,_名称);
contactMap.put(“编号”,_编号);
contactList.add(contactMap);
数字。添加(“+”+_数字);
Log.v(“+”计数器++,”编号+“+”名称);
}
电话。关闭();
}捕获(例外e){
//TODO:处理异常
}
最后{
如果(电话!=null){
电话。关闭();
}
}
}
然而,在类似的问题中,人们建议使用以下方法。此方法输出64行。换句话说,无论第一个方法返回什么,它都会循环8次:

public void getContactList() {
    contactList.clear();

    String phoneNum[] = null;
    Cursor cur = null;
    Cursor pCur = null;
    try {
        ContentResolver cr = getContentResolver();
        cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        int i = 0;

        if (cur.getCount() > 0) {

            phoneNum = new String[cur.getCount()];

            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    // get the phone number
                    pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            null,
                            null,
                            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

                    while (pCur.moveToNext()) {
                        // int _id = i;
                        String _name = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String _number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        _number = _number.replaceAll("[\\D]", "");

                        HashMap<String , String> contactMap= new HashMap<String, String>();

                        contactMap.put("NAME", _name);
                        contactMap.put("NUMBER", _number);

                        contactList.add(contactMap);
                        phoneNum[i] = _number;

                        Log.v("" + counter++, _number + " " + _name);
                    }
                    pCur.close();
                }
                i++;
            }
        }
        cur.close();
    } catch ( Exception e ) {
        // TODO: handle exception
    }
    finally {
        if(pCur != null){
            pCur.close();
        }
        if(cur != null){
            cur.close();
        }
    }
}
public void getContactList(){
contactList.clear();
字符串phoneNum[]=null;
游标cur=null;
光标pCur=null;
试一试{
ContentResolver cr=getContentResolver();
cur=cr.query(ContactsContract.commondatatypes.Phone.CONTENT\u URI,null,null,null);
int i=0;
如果(cur.getCount()>0){
phoneNum=新字符串[cur.getCount()];
while(cur.moveToNext()){
字符串id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._id));
字符串名称=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_name));
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0{
//获取电话号码
pCur=cr.query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
无效的
无效的
Contacts contract.commonDataTypes.Phone.DISPLAY_NAME+“ASC”);
while(pCur.moveToNext()){
//int _id=i;
String _name=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name));
String _number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataTypes.Phone.number));
_数字=_number.replaceAll(“[\\D]”,“”);
HashMap contactMap=新建HashMap();
contactMap.put(“名称”,_名称);
contactMap.put(“编号”,_编号);
contactList.add(contactMap);
phoneNum[i]=\u编号;
Log.v(“+”计数器++,”编号+“+”名称);
}
pCur.close();
}
i++;
}
}
cur.close();
}捕获(例外e){
//TODO:处理异常
}
最后{
如果(pCur!=null){
pCur.close();
}
如果(cur!=null){
cur.close();
}
}
}

如果人们建议第二种方法,他们应该考虑我没有注意到的一点。然而,我还没有发现这一点。当然,我无意无缘无故地将我的联系人循环七次。为什么人们说第二种方法更好?这是什么样的帮助或验证?

如果你只想显示所有可见联系人的姓名和电话号码,你的方法是完美的

第二种方法在将联系人的多个电话号码分组到单个数据结构中时非常有用,但您遗漏的是,第二个查询还应该将联系人id作为过滤器,否则您只是查询所有联系人的所有电话号码,而不是查询单个联系人的所有电话号码