Android之使用ContentResolver对通信录中的数据进行简单操作

1.加入读写权限

Xhtml代码 复制代码
  1. <uses-permission android:name="android.permission.READ_CONTACTS" />
  2. <uses-permission android:name="android.permission.WRITE_CONTACTS" />

<uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" />

联系人信息Uri:

content://com.android.contacts/contacts

联系人电话Uri:

content://com.android.contacts/data/phones

联系人Email Uri:

content://com.android.contacts/data/emails


(推荐)也可以这样获取联系人信息Uri:Uri uri = ContactsContract.Contacts.CONTENT_URI;



2.查询与添加联系人的操作(单元测试用例)


Java代码 复制代码
  1. public class ContactTest extends AndroidTestCase
  2. {
  3. private static final String TAG = "ContactTest";

  4. /**
  5. * 获取通讯录中所有联系人的简单信息
  6. * @throws Throwable
  7. */
  8. public void testGetAllContact() throws Throwable
  9. {
  10. //获取联系人信息的Uri
  11. Uri uri = ContactsContract.Contacts.CONTENT_URI;
  12. //获取ContentResolver
  13. ContentResolver contentResolver = this.getContext().getContentResolver();
  14. //查询数据,返回Cursor
  15. Cursor cursor = contentResolver.query(uri, null, null, null, null);
  16. while(cursor.moveToNext())
  17. {
  18. StringBuilder sb = new StringBuilder();
  19. //获取联系人的ID
  20. String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
  21. //获取联系人的姓名
  22. String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  23. //构造联系人信息
  24. sb.append("contactId=").append(contactId).append(",Name=").append(name);
  25. //查询电话类型的数据操作
  26. Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  27. null,
  28. ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
  29. null, null);
  30. while(phones.moveToNext())
  31. {
  32. String phoneNumber = phones.getString(phones.getColumnIndex(
  33. ContactsContract.CommonDataKinds.Phone.NUMBER));
  34. //添加Phone的信息
  35. sb.append(",Phone=").append(phoneNumber);
  36. }
  37. phones.close();

  38. //查询Email类型的数据操作
  39. Cursor emails = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
  40. null,
  41. ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
  42. null, null);
  43. while (emails.moveToNext())
  44. {
  45. String emailAddress = emails.getString(emails.getColumnIndex(
  46. ContactsContract.CommonDataKinds.Email.DATA));
  47. //添加Email的信息
  48. sb.append(",Email=").append(emailAddress);
  49. }
  50. emails.close();
  51. Log.i(TAG, sb.toString());
  52. }
  53. cursor.close();
  54. }

  55. /**添加联系人的第一种方法:
  56. * 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId
  57. * 这时后面插入data表的依据,只有执行空值插入,才能使插入的联系人在通讯录里面可见
  58. */
  59. public void testInsert()
  60. {
  61. ContentValues values = new ContentValues();
  62. //首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId
  63. Uri rawContactUri = this.getContext().getContentResolver().insert(RawContacts.CONTENT_URI, values);
  64. //获取id
  65. long rawContactId = ContentUris.parseId(rawContactUri);
  66. //往data表入姓名数据
  67. values.clear();
  68. values.put(Data.RAW_CONTACT_ID, rawContactId); //添加id
  69. values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);//添加内容类型(MIMETYPE)
  70. values.put(StructuredName.GIVEN_NAME, "凯风自南");//添加名字,添加到first name位置
  71. this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
  72. //往data表入电话数据
  73. values.clear();
  74. values.put(Data.RAW_CONTACT_ID, rawContactId);
  75. values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
  76. values.put(Phone.NUMBER, "13921009789");
  77. values.put(Phone.TYPE, Phone.TYPE_MOBILE);
  78. this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
  79. //往data表入Email数据
  80. values.clear();
  81. values.put(Data.RAW_CONTACT_ID, rawContactId);
  82. values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
  83. values.put(Email.DATA, "[email protected]");
  84. values.put(Email.TYPE, Email.TYPE_WORK);
  85. this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
  86. }

  87. /**添加联系人的第二种方法:
  88. * 批量添加联系人
  89. * @throws Throwable
  90. */
  91. public void testSave() throws Throwable
  92. {
  93. //官方文档位置:reference\android\provider\ContactsContract.RawContacts.html
  94. //建立一个ArrayList存放批量的参数
  95. ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
  96. int rawContactInsertIndex = ops.size();
  97. ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
  98. .withValue(RawContacts.ACCOUNT_TYPE, null)
  99. .withValue(RawContacts.ACCOUNT_NAME, null)
  100. .build());
  101. //官方文档位置:reference\android\provider\ContactsContract.Data.html
  102. //withValueBackReference后退引用前面联系人的id
  103. ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
  104. .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
  105. .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
  106. .withValue(StructuredName.GIVEN_NAME, "小明")
  107. .build());
  108. ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
  109. .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
  110. .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
  111. .withValue(Phone.NUMBER, "13671323809")
  112. .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
  113. .withValue(Phone.LABEL, "手机号")
  114. .build());
  115. ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
  116. .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
  117. .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
  118. .withValue(Email.DATA, "[email protected]")
  119. .withValue(Email.TYPE, Email.TYPE_WORK)
  120. .build());
  121. ContentProviderResult[] results = this.getContext().getContentResolver()
  122. .applyBatch(ContactsContract.AUTHORITY, ops);
  123. for(ContentProviderResult result : results)
  124. {
  125. Log.i(TAG, result.uri.toString());
  126. }
  127. }
  128. }

你可能感兴趣的:(Android之使用ContentResolver对通信录中的数据进行简单操作)