获取联系人

/**
 * 获取联系人
 * @return
 */
public static List<ContactInfo> getContactInfos(Context context) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
    Uri dataUri = Uri.parse("content://com.android.contacts/data");
    List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
    Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
            null, null, null);
    while (cursor.moveToNext()) {
        String id = cursor.getString(0);
        if (id != null) {
            ContactInfo info = new ContactInfo();
            Cursor dataCursor = resolver.query(dataUri, new String[] {
                    "mimetype", "data1" }, "raw_contact_id=?",
                    new String[] { id }, null);
            while (dataCursor.moveToNext()) {
                if("vnd.android.cursor.item/name".equals( dataCursor.getString(0))){
                    info.setName(dataCursor.getString(1));
                }else if("vnd.android.cursor.item/phone_v2".equals( dataCursor.getString(0))){
                    info.setPhone(dataCursor.getString(1));
                }
            }
            contactInfos.add(info);
            dataCursor.close();
        }
    }
    cursor.close();
    return contactInfos;
}

 

要使用到这三个表,但是谷歌内部在查询这个data表的时候内部使用的并不是查询data表而是查询了data表的视图,这个视图中直接将mime_Type类型给关联好了,所以这里直接查询的就是mimetype这个类型就可以了,还有就是如果自己的手机中如果删除了一个联系人在查询的时候就会报错,这里因为是在手机中删除一个联系人的时候并不是清空数据库中的数据,而是将这个raw_contacks中的id置为null,所以会报错,这里就要进行判断一下查出来的id是否为null。

 

转载于:https://www.cnblogs.com/huangzx/p/4481076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值