获取手机通讯录信息

写一个类获取手机通讯录信息,方便以后直接调用。

获取手机通讯录的类GetContactsInfo

public class GetContactsInfo {
    List<ContactsInfo> localList;
    Context context;
    ContactsInfo contactsInfo;
    /** 获取库Phon表字段 **/
    private static final String[] PHONES_PROJECTION = new String[] {
            Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID,
            Phone.SORT_KEY_PRIMARY };

    /** 联系人显示名称 **/
    private static final int PHONES_DISPLAY_NAME_INDEX = 0;

    /** 电话号码 **/
    private static final int PHONES_NUMBER_INDEX = 1;

    /** 头像ID **/
    private static final int PHONES_PHOTO_ID_INDEX = 2;

    /** 联系人的ID **/
    private static final int PHONES_CONTACT_ID_INDEX = 3;
    /**
     * Sort key that takes into account locale-based traditions for sorting
     * names in address books. The default sort key is
     * {@link #DISPLAY_NAME_PRIMARY}. For Chinese names the sort key is the
     * name's Pinyin spelling, and for Japanese names it is the Hiragana version
     * of the phonetic name.
     */
    private static final int PHONES_CONTACT_SORT_KEY = 4;

    public GetContactsInfo(Context context) {
        localList = new ArrayList<ContactsInfo>();
        this.context = context;

    }

    /** 得到手机通讯录联系人信息 **/
    public List<ContactsInfo> getPhoneContacts() {
        ContentResolver resolver = context.getContentResolver();

        // 获取手机联系人
        // 然后在根据SORT_KEY_PRIMARY排序
        Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
                PHONES_PROJECTION, null, null, Phone.SORT_KEY_PRIMARY);

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

                // 得到手机号码
                String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
                phoneNumber = phoneNumber.replaceAll(" ", "");// 去掉空格
                phoneNumber = phoneNumber.replaceAll("-", "");// 去掉-符号
                // 当手机号码为空的或者为空字段 跳过当前循环
                if (TextUtils.isEmpty(phoneNumber))
                    continue;

                // 得到联系人名称
                String contactName = phoneCursor
                        .getString(PHONES_DISPLAY_NAME_INDEX);

                // 得到联系人ID
                Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);

                // 取得联系人Sort_key
                String sortKey = getSortKey(phoneCursor
                        .getString(PHONES_CONTACT_SORT_KEY));

                // 得到联系人头像ID
                Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);

                // 得到联系人头像Bitamp
                Bitmap contactPhoto = null;

                // photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
                if (photoid > 0) {
                    Uri uri = ContentUris.withAppendedId(
                            ContactsContract.Contacts.CONTENT_URI, contactid);
                    InputStream input = ContactsContract.Contacts
                            .openContactPhotoInputStream(resolver, uri);
                    contactPhoto = BitmapFactory.decodeStream(input);
                } else {
                    contactPhoto = BitmapFactory.decodeResource(
                            context.getResources(), R.drawable.ic_launcher);
                }
                ContactsInfo contactsInfo = new ContactsInfo();
                contactsInfo.setBitmap(contactPhoto);
                contactsInfo.setContactsName(contactName);
                contactsInfo.setContactsPhone(phoneNumber);
                contactsInfo.setSortKey(sortKey);
                localList.add(contactsInfo);
            }

            phoneCursor.close();
        }

        return localList;
    }

    /**
     * 获取sort key的首个字符,如果是英文字母就直接返回,否则返回#。
     * 
     * @param sortKeyString
     *            数据库中读取出的sort key
     * @return 英文字母或者#
     */
    private static String getSortKey(String sortKeyString) {
        String key = sortKeyString.substring(0, 1).toUpperCase();
        if (key.matches("[A-Z]")) {
            return key;
        }
        return "#";
    }
}

手机通讯录显示的信息的实体类

public class ContactsInfo implements Serializable{
private String ContactsPhone;
private String ContactsName;
private Bitmap Bitmap;
private String sortKey;
private int  contact_id;
public String getSortKey() {
    return sortKey;
}
public void setSortKey(String sortKey) {
    this.sortKey = sortKey;
}
public int getContact_id() {
    return contact_id;
}
public void setContact_id(int contact_id) {
    this.contact_id = contact_id;
}
public Bitmap getBitmap() {
    return Bitmap;
}
public void setBitmap(Bitmap bitmap) {
    Bitmap = bitmap;
}
public String getContactsPhone() {
    return ContactsPhone;
}
public void setContactsPhone(String contactsPhone) {
    ContactsPhone = contactsPhone;
}
public String getContactsName() {
    return ContactsName;
}
public void setContactsName(String contactsName) {
    ContactsName = contactsName;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值