随笔 - 91,  文章 - 0,  评论 - 1,  阅读 - 87621
复制代码
/**
 * 获取联系人
 * @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。

 

posted on   道无涯  阅读(268)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示