android 读取通讯录

来源:互联网 发布:新开淘宝店铺怎样做 编辑:程序博客网 时间:2024/05/21 19:45
 private ArrayList<ContactBean> getAllContacts() {
        ArrayList<ContactBean> arrayList = new ArrayList<ContactBean>();


        phoneMap = new HashMap<String, Boolean>();
        //获取本机联系人
        Cursor cur = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI,
                null,
                null,
                null,
                ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC");
        if (cur.moveToFirst()) {
            do {
                ContactBean samContact = new ContactBean();
                samContact.setSelected(false);
                int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
                int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                // 获得联系人的ID号
                String contactId = cur.getString(idColumn);
                // 获得联系人姓名
                String disPlayName = cur.getString(displayNameColumn);
                System.out.println(disPlayName);
                samContact.contactName = disPlayName;
                // 查看该联系人有多少个电话号码。如果没有这返回值为0
                int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                if (phoneCount < 1) {
                    continue;
                }
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                if (phones != null && phones.moveToFirst()) {
                    do {
                        // 遍历所有的电话号码
                        String phoneNumber = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        phoneNumber = phoneNumber.trim();
                        phoneNumber = phoneNumber.replaceAll(" ", "");
                        int phoneType = phones
                                .getInt(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                        if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
                            Boolean hasAddPhoneNumber = false;
                            hasAddPhoneNumber = phoneMap.get(phoneNumber);
                            if (hasAddPhoneNumber != null && hasAddPhoneNumber) {
                                break;
                            } else {
                                samContact.contactPhoneNumber = phoneNumber;


                                //设置选中
                                if (defaultSelectList != null) {
                                    int count = defaultSelectList.size();
                                    for (int kk = 0; kk < count; kk++) {
                                        if (defaultSelectList.get(kk).getContactPhoneNumber().equals(samContact.getContactPhoneNumber())) {
                                            samContact.setSelected(true);
                                            break;
                                        }
                                    }
                                }




                                arrayList.add(samContact);
                                phoneMap.put(phoneNumber, true);
                            }
                            break;
                        }
                    } while (phones.moveToNext());


                    if (phones != null) {
                        phones.close();
                    }


                }




            } while (cur.moveToNext());
        }


        if (cur != null) {
            cur.close();
        }
        //获取sim卡联系人
        Uri uri = Uri.parse("content://icc/adn");
        Cursor cur2 = getContentResolver().query(
                uri,
                null,
                null,
                null,
                ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC");
//        System.out.println("contact num from sim card = "+cur2.getCount());
//        System.out.println("---------------");
        if (cur2 != null && cur2.moveToFirst()) {
            do {
                try {
                    ContactBean samContact = new ContactBean();
                    samContact.setSelected(false);
                    int displayNameColumn = cur2.getColumnIndex(Contacts.People.NAME);
                    int phoneColumn = cur2.getColumnIndex(Contacts.People.NUMBER);


                    String phoneNumber = cur2.getString(phoneColumn);
                    phoneNumber = phoneNumber.trim();
                    phoneNumber = phoneNumber.replaceAll(" ", "");
                    samContact.contactName = cur2.getString(displayNameColumn);
                    if (samContact.contactName == null) {
                        continue;
                    }
                    samContact.contactPhoneNumber = phoneNumber;
                    if (samContact.contactPhoneNumber == null) {
                        continue;
                    }
                    Boolean hasAddPhoneNumber = false;
                    hasAddPhoneNumber = phoneMap.get(samContact.contactPhoneNumber);
                    if (hasAddPhoneNumber != null && hasAddPhoneNumber) {
                        continue;
                    } else {


                        //设置选中
                        if (defaultSelectList != null) {
                            int count = defaultSelectList.size();
                            for (int kk = 0; kk < count; kk++) {
                                if (defaultSelectList.get(kk).getContactPhoneNumber().equals(samContact.getContactPhoneNumber())) {
                                    samContact.setSelected(true);
                                    break;
                                }
                            }
                        }


                        arrayList.add(samContact);
                        phoneMap.put(samContact.getContactPhoneNumber(), true);
                    }
                    arrayList.add(samContact);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } while (cur2.moveToNext());
        }


        if (cur2 != null) {
            cur2.close();
        }


        return arrayList;
    }


public class ContactBean implements Parcelable {
    public String contactName;
    public String contactPhoneNumber;
    private boolean isSelected;




    public boolean isSelected() {
        return isSelected;
    }


    public void setSelected(boolean selected) {
        isSelected = selected;
    }


    public String getContactPhoneNumber() {
        return contactPhoneNumber;
    }


    public void setContactPhoneNumber(String contactPhoneNumber) {
        this.contactPhoneNumber = contactPhoneNumber;
    }


    public String getContactName() {
        return contactName;
    }


    public void setContactName(String contactName) {
        this.contactName = contactName;
    }


    @Override
    public int describeContents() {
        return 0;
    }


    @Override
    public void writeToParcel(Parcel parcel, int i) {
        int isChecked = isSelected?1:0;
        parcel.writeInt(isChecked);
        parcel.writeString(contactName);
        parcel.writeString(contactPhoneNumber);
    }




    public static final Parcelable.Creator CREATOR = new Creator() {


        @Override
        public ContactBean createFromParcel(Parcel source) {


            // TODO Auto-generated method stub
            ContactBean parcelableObj = new ContactBean();
            parcelableObj.isSelected=source.readInt()==1?true:false;
            parcelableObj.contactName=source.readString();
            parcelableObj.contactPhoneNumber = source.readString();
            return parcelableObj;


        }


        @Override
        public ContactBean[] newArray(int size) {


            return new ContactBean[size];


        }


    };
}