• 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);
  • 相关阅读:
    2018-11-28笔记
    2018-11-27笔记
    2018-11-26笔记
    DBUtils和连接池
    动态页面技术(JSP/EL/JSTL)
    会话技术Cookie&Session
    JavaEE—— HttpServletRequest
    JavaEE—— HttpServletResponse
    JavaWeb核心之Servlet
    JavaEE——HTTP协议和Tomcat服务器
  • 原文地址:https://www.cnblogs.com/zzl521/p/8945251.html
Copyright © 2020-2023  润新知