Android中读取手机联系人

主要就是用 内容解析者来处理问题:

首先应该先清楚Android手机联系人的数据库:读取主要用了3张表:

Table Name作用
contacts记录联系人id,(有几列即表示有几个联系人)
mimeTypes存储类型比对(为了节省数据库的空间)
data联系人信息保存在此表中

但是在查的过程中,第一次我们是查的表contacts,第二次我们查的是view_data这个视图

查询过程中,第一循环有几个联系人就执行几次,但是第二次循环每个联系人最多执行11次,依据你的联系人存储信息不同而不同的

一段代码飘过:

ContentResolver contentResolver = getContentResolver();
                // 用cursor对象查询
                Cursor cursor = contentResolver.query(Uri
                        .parse("content://com.android.contacts/raw_contacts"), 
                        new String[] { "contact_id" }, null, // 查询条件 "a=?"
                        null, // 提供问号的值
                        null);
                //  循环游标
                while (cursor.moveToNext()) {
                    // cursor的索引值从0开始的
                    String id = cursor.getString(0);
                    // 根据唯一性id值,查询data表和mimetype生成的视图,获取data以及mimetype字段
                    Cursor indexCursor = contentResolver.query(
                            Uri.parse("content://com.android.contacts/data"),
                            new String[] { "data1", "mimetype" },
                            "raw_contact_id=?", new String[] { id }, null);
                    // 5 循环遍历游标的值  查询过程还是不太清楚
                    HashMap<String, String> hm = new HashMap<String, String>();
                    while (indexCursor.moveToNext()) {
//                        System.out.println(indexCursor.getString(0) +" "+ indexCursor.getString(1));
                        hm.put(indexCursor.getString(1),indexCursor.getString(0));
                    }
                    contactList.add(hm);
                    indexCursor.close();
                }
                // 关闭游标
                cursor.close();
                //子线程里发送更新ui的数据
                Message msg = new Message();
                msg.what = 1;
                mHandler.sendMessage(msg);

 

转载于:https://www.cnblogs.com/zzl521/p/8945251.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值