authorities :当局
1、到开发环境data/data/目录下 com.android.providers.contacts/databases/导出到左面contacts2.db
2、用工具打开数据库关心三张表:raw_contacts、data、mimetypes
3、创建一个新的工程去读取联系人SelectContact
4、创建一个得到所有联系人的方法 getContactInfos();----表的路径
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri datauri = Uri.parse("content://com.android.contacts/data");
5、查询raw_contact表取联系人id
Cursor cursor =resolver.query(uri, new String[] { "contact_id" },
null, null, null);
6、得到联系人的ID
while (cursor.moveToNext()) {
String id = cursor.getString(0);
System.out.println("联系人的id为:" + id);
}
7、查表data表到联系人的姓名和电话号码
Map<String, String> map = new HashMap<String, String>();
// 查询data表 把当前联系人的姓名和电话new String[]{"data1","mimetype"}数据给取出来.
Cursor dataCursor = resolver.query(datauri, new String[]{"data1","mimetype"},
"raw_contact_id=?", new String[] { id }, null);
8、添加数据到map集合
String data1 =dataCursor.getString(0);
String mimetype =dataCursor.getString(1);
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
System.out.println("电话:" + data1);
map.put("phone", data1);
} else if ("vnd.android.cursor.item/name".equals(mimetype)) {
System.out.println("姓名:" + data1);
map.put("name", data1);
}
9、读取联系人需要加权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
private List<Map<String, String>> getContactInfo() { // 把所有的联系人 List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // 得到一个内容解析器 ContentResolver resolver = getContentResolver(); // raw_contacts uri Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri uriData = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String contact_id = cursor.getString(0); if (contact_id != null) { // 具体的某一个联系人 Map<String, String> map = new HashMap<String, String>(); Cursor dataCursor = resolver.query(uriData, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contact_id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); System.out.println("data1==" + data1 + "==mimetype==" + mimetype); if ("vnd.android.cursor.item/name".equals(mimetype)) { // 联系人的姓名 map.put("name", data1); } else if ("vnd.android.cursor.item/phone_v2" .equals(mimetype)) { // 联系人的电话号码 map.put("phone", data1); } } list.add(map); dataCursor.close(); } } cursor.close(); return list; }