ContentResolver resolver = getContentResolver();
// 1. 查询raw_contacts表,把联系人的id取出来
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri datauri = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(0);
if (id != null) {
System.out.println("联系人id:" + id);
// 2. 根据联系人的id,查询data表,把这个id的数据取出来
// 系统api 查询data表的时候 不是真正的查询data表 而是查询的data表的视图
Cursor dataCursor = resolver.query(datauri, new String[] {
"data1", "mimetype" }, "raw_contact_id=?",
new String[] { id }, null);
ContactInfo info = new ContactInfo();
while (dataCursor.moveToNext()) {
String data1 = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
if ("vnd.android.cursor.item/name".equals(mimetype)) {
System.out.println("姓名=" + data1);
} else if ("vnd.android.cursor.item/email_v2"
.equals(mimetype)) {
System.out.println("邮箱=" + data1);
} else if ("vnd.android.cursor.item/phone_v2"
.equals(mimetype)) {
info.phone = data1;
System.out.println("电话=" + data1);
}
} else if ("vnd.android.cursor.item/im".equals(mimetype)){
System.out.println("QQ=" + data1);
}
}
dataCursor.close();
}
}
cursor.close();
Android 获取手机联系人代码
最新推荐文章于 2023-12-20 21:05:50 发布