三.代码

3.1根据API写代码.

在api里 ContactsContract.Data 和ContactsContract.RawContacts文档里有关于insert ,update, delete,query的代码,显示出操作各自的表的代码。可以根据这些来完成我们自己的逻辑。

3.2 查询 (查出全部联系人,在只显示姓名)需要如图:


 

需求分析:由于列表中只需要姓名,所以在查询表时就只查询出姓名信息就好。当点击某个联系人再查出Email,phone等详细信息。

3.2.1 查询联系人总表代码:

说明:由于姓名可以直接在ContactsContract.Contacts表里查到,所以如下

 
  
  1. public static final String[] PROJECTION_CONTACTS = { Contacts._ID,
  2. Contacts.PHOTO_ID, Contacts.IN_VISIBLE_GROUP,
  3. Contacts.HAS_PHONE_NUMBER, Contacts.DISPLAY_NAME,
  4. Contacts.CUSTOM_RINGTONE };
  5. /**
  6. * wu0wu
  7. *
  8. * 功能:查询所有联系人PROJECTION_CONTACTS信息
  9. *
  10. * */
  11. public static void _getContacts(ContentResolver cr) {
  12. Cursor cursorContact = null;
  13. try {
  14. cursorContact = cr.query(ContactsContract.Contacts.CONTENT_URI,
  15. PROJECTION_CONTACTS, Contacts.IN_VISIBLE_GROUP + "=1",
  16. null, null);
  17. Log.e("wu0wu", "联系人个数=" + cursorContact.getCount());
  18. int[] indexs = getColumnIndexs(PROJECTION_CONTACTS, cursorContact);
  19. while (cursorContact.moveToNext()) {
  20. Log.e("wu0wu", "------------------------------------");
  21. for (int i = 0; i < PROJECTION_CONTACTS.length; i++) {
  22. String value = cursorContact.getString(indexs[i]);
  23. Log.e("wu0wu", PROJECTION_CONTACTS[i] + "=" + value);
  24. }
  25. }
  26. } catch (Exception e) {
  27. Log.e("wu0wu", e.toString());
  28. } finally {
  29. if (cursorContact != null) {
  30. cursorContact.close();
  31. }
  32. }
  33. }
  34. private static int[] getColumnIndexs(String[] projections, Cursor c) {
  35. int[] ret = new int[projections.length];
  36. for (int i = 0; i < projections.length; i++) {
  37. ret[i] = c.getColumnIndex(projections[i]);
  38. }
  39. return ret;
  40. }

3.2.2 根据contactId查询联系人详细

 
  
  1. // phone
  2. private static final String[] PROJECTION_PHONENUMBER_CONTACT = {
  3. Phone.NUMBER, Phone.TYPE, Phone.LABEL };
  4. /* DISPLAY_NAME唯一性 */
  5. private static final String[] PROJECTION_DISPLAYNAME_CONTACT = { StructuredName.DISPLAY_NAME };
  6. // Email
  7. private static final String[] PROJECTION_EAMIL_CONTACT = { Email.DATA1,
  8. Email.TYPE, Email.LABEL };
  9. // IM
  10. private static final String[] PROJECTION_IM_CONTACT = new String[] {
  11. Im.DATA, Im.TYPE, Im.LABEL, Im.PROTOCOL };
  12. // address
  13. private static final String[] PROJECTION_ADDRESS_CONTACT = new String[] {
  14. StructuredPostal.STREET, StructuredPostal.CITY,
  15. StructuredPostal.REGION, StructuredPostal.POSTCODE,
  16. StructuredPostal.COUNTRY, StructuredPostal.TYPE,
  17. StructuredPostal.LABEL, StructuredPostal.POBOX,
  18. StructuredPostal.NEIGHBORHOOD, };
  19. // Organization
  20. private static final String[] PROJECTION_ORGANIZATION_CONTACT = new String[] {
  21. Organization.COMPANY, Organization.TYPE, Organization.LABEL,
  22. Organization.TITLE };
  23. // note
  24. private static final String[] PROJECTION_NOTES_CONTACT = new String[] { Note.NOTE };
  25. // nickname
  26. private static final String[] PROJECTION_NICKNAMES_CONTACT = new String[] {
  27. Nickname.NAME, Nickname.TYPE, Nickname.LABEL };
  28. // website
  29. private static final String[] PROJECTION_WEBSITES_CONTACT = new String[] {
  30. Website.URL, Website.TYPE, Website.LABEL };
  31. /**
  32. * 功能:根据contactId查询联系人详细
  33. *
  34. * 在android.provider.ContactsContract.Data表里查询
  35. * */
  36. public static void _getContactByContactId(ContentResolver cr,
  37. String contactId) {
  38. Cursor c = null;
  39. c = cr.query(Data.CONTENT_URI, null, Data.CONTACT_ID + "=?",
  40. new String[] { contactId }, null);
  41. String mimeType = null;
  42. String[] contentValue = null;
  43. ArrayList<String[]> displayNameList = new ArrayList<String[]>();// 存显示名
  44. ArrayList<String[]> phoneList = new ArrayList<String[]>();// 存电话号码,可多个
  45. ArrayList<String[]> emailList = new ArrayList<String[]>();// 存Email,可多个
  46. ArrayList<String[]> imList = new ArrayList<String[]>();// 存im,可多个
  47. ArrayList<String[]> postalList = new ArrayList<String[]>();// 存postal地址,可多个
  48. ArrayList<String[]> organizationList = new ArrayList<String[]>();// 存organization组织,可多个
  49. ArrayList<String[]> noteList = new ArrayList<String[]>();// 存note备注
  50. ArrayList<String[]> nicknameList = new ArrayList<String[]>();// 存Nickname昵称
  51. ArrayList<String[]> websiteList = new ArrayList<String[]>();// 存Website网站
  52. while (c.moveToNext()) {
  53. // 根据mimeType分类信息
  54. mimeType = c.getString(c.getColumnIndex(Data.MIMETYPE));
  55. if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
  56. contentValue = MyUtils.getStringInContactCursor(c,
  57. PROJECTION_DISPLAYNAME_CONTACT);
  58. displayNameList.add(contentValue);
  59. } else if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) {
  60. // 每个contentValue存一类PROJECTION_PHONENUMBER_CONTACT数据
  61. contentValue = MyUtils.getStringInContactCursor(c,
  62. PROJECTION_PHONENUMBER_CONTACT);
  63. phoneList.add(contentValue);
  64. } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
  65. contentValue = MyUtils.getStringInContactCursor(c,
  66. PROJECTION_EAMIL_CONTACT);
  67. emailList.add(contentValue);
  68. } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType)) {
  69. contentValue = MyUtils.getStringInContactCursor(c,
  70. PROJECTION_IM_CONTACT);
  71. imList.add(contentValue);
  72. } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) {
  73. contentValue = MyUtils.getStringInContactCursor(c,
  74. PROJECTION_ADDRESS_CONTACT);
  75. postalList.add(contentValue);
  76. } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
  77. contentValue = MyUtils.getStringInContactCursor(c,
  78. PROJECTION_ORGANIZATION_CONTACT);
  79. organizationList.add(contentValue);
  80. } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType)) {
  81. contentValue = MyUtils.getStringInContactCursor(c,
  82. PROJECTION_NOTES_CONTACT);
  83. noteList.add(contentValue);
  84. } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) {
  85. contentValue = MyUtils.getStringInContactCursor(c,
  86. PROJECTION_NICKNAMES_CONTACT);
  87. nicknameList.add(contentValue);
  88. } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType)) {
  89. contentValue = MyUtils.getStringInContactCursor(c,
  90. PROJECTION_WEBSITES_CONTACT);
  91. websiteList.add(contentValue);
  92. }
  93. }
  94. c.close();
  95. // log
  96. MyUtils.logContactsDetails("displayName",PROJECTION_DISPLAYNAME_CONTACT, displayNameList);
  97. MyUtils.logContactsDetails("phoneNumber",PROJECTION_PHONENUMBER_CONTACT, phoneList);
  98. MyUtils.logContactsDetails("Email", PROJECTION_EAMIL_CONTACT,emailList);
  99. MyUtils.logContactsDetails("IM", PROJECTION_IM_CONTACT, imList);
  100. MyUtils.logContactsDetails("Address", PROJECTION_ADDRESS_CONTACT,postalList);
  101. MyUtils.logContactsDetails("Organization",PROJECTION_ORGANIZATION_CONTACT, organizationList);
  102. MyUtils.logContactsDetails("Note", PROJECTION_NOTES_CONTACT, noteList);
  103. MyUtils.logContactsDetails("NickName", PROJECTION_NICKNAMES_CONTACT,nicknameList);
  104. MyUtils.logContactsDetails("WebSit", PROJECTION_WEBSITES_CONTACT,websiteList);
  105. }

用到的两个方法:

 
  
  1. public static String[] getStringInContactCursor(Cursor c,
  2. String[] projection) {
  3. String[] contentValue = new String[projection.length];
  4. for (int i = 0; i < contentValue.length; i++) {
  5. String value = c.getString(c.getColumnIndex(projection[i]));
  6. if (value == null) {
  7. contentValue[i] = "";
  8. } else {
  9. contentValue[i] = value;
  10. }
  11. }
  12. return contentValue;
  13. }
  14. public static void logContactsDetails(String title, String[] projection,
  15. ArrayList<String[]> data) {
  16. Log.e("wu0wu", "--------" + title + "--------");
  17. for (int i = 0; i < data.size(); i++) {
  18. for (int j = 0; j < data.get(i).length; j++) {
  19. Log.e("wu0wu", projection[j] + "=" + data.get(i)[j]);
  20. }
  21. }
  22. }

3.3 新建联系人

接口方法:

 
  
  1. /**
  2. * 新建联系人的接口
  3. *
  4. * @param String
  5. * accountName,accountType 为账号名账号类型,一般为NULL
  6. * @throws RemoteException
  7. * @throws OperationApplicationException
  8. */
  9. public static String _insertContact(ContentResolver cr, String accountName,
  10. String accountType, String displayName, ArrayList<String[]> phone,
  11. ArrayList<String[]> email, ArrayList<String[]> im,
  12. ArrayList<String[]> address, ArrayList<String[]> organization,
  13. ArrayList<String[]> notes, ArrayList<String[]> nickname,
  14. ArrayList<String[]> website) throws RemoteException,
  15. OperationApplicationException {
  16. ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
  17. String rawId = "";
  18. long rawContactId = insertRawContact(cr, accountName, accountType);
  19. rawId = Long.toString(rawContactId);
  20. if (displayName != null) {
  21. insertContactDisplayname(ops, StructuredName.CONTENT_ITEM_TYPE,
  22. rawId, displayName);
  23. }
  24. if (phone != null) {
  25. for (int j = 0; j < phone.size(); j++) {
  26. String[] item = phone.get(j);
  27. insertItemToContact(ops, Phone.CONTENT_ITEM_TYPE, rawId,
  28. PROJECTION_PHONENUMBER_CONTACT, item);
  29. }
  30. }
  31. if (email != null) {
  32. for (int j = 0; j < email.size(); j++) {
  33. String[] item = email.get(j);
  34. insertItemToContact(ops, Email.CONTENT_ITEM_TYPE, rawId,
  35. PROJECTION_EAMIL_CONTACT, item);
  36. }
  37. }
  38. if (im != null) {
  39. for (int j = 0; j < im.size(); j++) {
  40. String[] item = im.get(j);
  41. insertItemToContact(ops, Im.CONTENT_ITEM_TYPE, rawId,
  42. PROJECTION_IM_CONTACT, item);
  43. }
  44. }
  45. if (address != null) {
  46. for (int j = 0; j < address.size(); j++) {
  47. String[] item = address.get(j);
  48. insertItemToContact(ops, StructuredPostal.CONTENT_ITEM_TYPE,
  49. rawId, PROJECTION_ADDRESS_CONTACT, item);
  50. }
  51. }
  52. if (organization != null) {
  53. for (int j = 0; j < organization.size(); j++) {
  54. String[] item = organization.get(j);
  55. insertItemToContact(ops, Organization.CONTENT_ITEM_TYPE, rawId,
  56. PROJECTION_ORGANIZATION_CONTACT, item);
  57. }
  58. }
  59. if (notes != null) {
  60. for (int j = 0; j < notes.size(); j++) {
  61. String[] item = notes.get(j);
  62. insertItemToContact(ops, Note.CONTENT_ITEM_TYPE, rawId,
  63. PROJECTION_NOTES_CONTACT, item);
  64. }
  65. }
  66. if (nickname != null) {
  67. for (int j = 0; j < nickname.size(); j++) {
  68. String[] item = nickname.get(j);
  69. insertItemToContact(ops, Nickname.CONTENT_ITEM_TYPE, rawId,
  70. PROJECTION_NICKNAMES_CONTACT, item);
  71. }
  72. }
  73. if (website != null) {
  74. for (int j = 0; j < website.size(); j++) {
  75. String[] item = website.get(j);
  76. insertItemToContact(ops, Website.CONTENT_ITEM_TYPE, rawId,
  77. PROJECTION_WEBSITES_CONTACT, item);
  78. }
  79. }
  80. cr.applyBatch(ContactsContract.AUTHORITY, ops);
  81. return rawId;
  82. }
  83. /*
  84. * 通过往ROWCONTACT里插入数据,获得rawId
  85. *
  86. * @param cr
  87. *
  88. * @param accountName 一般为NULL
  89. *
  90. * @param accountType 一般为NULL
  91. *
  92. * @return
  93. */
  94. private static long insertRawContact(ContentResolver cr,
  95. String accountName, String accountType) {
  96. ContentValues values = new ContentValues();
  97. values.put(RawContacts.ACCOUNT_NAME, accountName);
  98. values.put(RawContacts.ACCOUNT_TYPE, accountType);
  99. // values.put(Contacts.DISPLAY_NAME, displayName);
  100. Uri rawContactUri = cr.insert(RawContacts.CONTENT_URI, values);
  101. long rawContactId = ContentUris.parseId(rawContactUri);
  102. return rawContactId;
  103. }
  104. private static void insertContactDisplayname(
  105. ArrayList<ContentProviderOperation> ops, String mimeType,
  106. String rawContactId, String displayName) throws RemoteException,
  107. OperationApplicationException {
  108. ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI).withValue(
  109. Data.MIMETYPE, mimeType).withValue(Data.RAW_CONTACT_ID,
  110. rawContactId).withValue(StructuredName.DISPLAY_NAME,
  111. displayName).build());
  112. }
  113. private static void insertItemToContact(
  114. ArrayList<ContentProviderOperation> ops, String mimeType,
  115. String rawContactId, String[] PROJECTION_CONTACT, String[] item)
  116. throws RemoteException, OperationApplicationException {
  117. // ContentValues values = new ContentValues();
  118. // values.put(Data.RAW_CONTACT_ID, rawContactId);
  119. // values.put(Data.MIMETYPE, mimeType);
  120. // for (int i = 0; i < PROJECTION_CONTACT.length; i++) {
  121. // values.put(PROJECTION_CONTACT[i], item[i]);
  122. // }
  123. // Uri dataUri = cr.insert(Data.CONTENT_URI, values);
  124. Builder builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
  125. builder.withYieldAllowed(true);
  126. builder.withValue(Data.RAW_CONTACT_ID, rawContactId);
  127. builder.withValue(Data.MIMETYPE, mimeType);
  128. for (int i = 0; i < PROJECTION_CONTACT.length; i++) {
  129. builder.withValue(PROJECTION_CONTACT[i], item[i]);
  130. }
  131. ops.add(builder.build());
  132. }