Android之获取联系人

Android入门中,记录学习中的遇到的问题和一些个人总结。

联系人数据库路径为:/data/data/com.android.providers.contacts/database/contacts2.db

权限问题:安卓6.0以上只是在XML中写权限还不够,代码中也需要写。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
  requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 10);
} public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  if (requestCode == 10) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      //有权限,可以进行获取联系人操作
    } else {
      Toast.makeText(this, "您阻止了权限", Toast.LENGTH_SHORT).show();
    }
}

第一种方法,代码也最简洁,SORT_KEY_PRIMARY还能按中文名字拼音排序,个人推荐:
 1 Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, Phone.SORT_KEY_PRIMARY);
2 if (cursor != null) {
3 while (cursor.moveToNext()) {
4 ContactBean contact = new ContactBean();
5 contact.setName(cursor.getString(0));
6 contact.setPhone(cursor.getString(1));
7 lists.add(contact);
8 }
9 cursor.close();
10 }

第二种方法:可根据联系人id获取相关信息

 1 Uri uri = Uri.parse("content://com.android.contacts/contacts");
2 ContentResolver resolver = context.getContentResolver();
3 Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);
4 if (cursor != null && cursor.getCount() > 0){
5 while (cursor.moveToNext()) {
6 Contact contact = new Contact();
7 int contactId = cursor.getInt(0);
8 uri = Uri.parse("content://com.android.contacts/contacts/" + contactId + "/data");
9 Cursor cursor1 = resolver.query(uri, new String[]{"mimetype", "data1"}, null, null, null);
10 if (cursor1 != null && cursor1.getCount() > 0){
11 while (cursor1.moveToNext()) {
12 String mimeType = cursor1.getString(0);
13 String data1 = cursor1.getString(1);
14 if ("vnd.android.cursor.item/name".equals(mimeType)) { //是姓名
15 contact.setName(data1);
16 } else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { //手机
17 contact.setPhone(data1);
18 }
19 }
20 cursor1.close();
21 lists.add(contact);
22 }
23 }
24 cursor.close();
25 }

第三种方法:跟第二种方法比较,只有第二个uri不同,(PS:此方法有BUG,获取到一部分人的数据为NULL,但联系人总数是一样的。据说是因为以前删除过联系人,本人手机未ROOT,也没法看数据库表的数据,希望有人能给个解释)

 1 Uri uri4Contacts = Uri.parse(ContactsContract.AUTHORITY_URI + "/contacts");
2 Uri uri4Data = Uri.parse(ContactsContract.AUTHORITY_URI + "/data");
3 List<Contact> lists = new ArrayList<>();
4 Cursor cursor1 = context.getContentResolver().query(uri4Contacts, new String[]{"_id"}, null, null, null);
5 if (cursor1.moveToNext()) {
6 Contact contact = new Contact();
7 String id = cursor1.getString(0);
8 Cursor cursor2 = context.getContentResolver().query(uri4Data, new String[]{"data1", "mimetype"}, "row_contact_id = ?", new String[]{id}, null);
9 if (cursor2.moveToNext()) {
10 String data = cursor2.getString(0);
11 String mimetype = cursor2.getString(1);
12 if (mimetype.equals("vnd.android.cursor.item/name")) {
13 contact.setName(data);
14 } else if (mimetype.equals("vnd.android.cursor.item/phone_v2")) {
15 contact.setPhone(data);
16 }
17 }
18 }
上一篇:第一章 了解Web及网络基础


下一篇:QT 程序自定义插件