ContactManager.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package me.yoqi.deleteallcontacts.utils;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import android.content.ContentProviderOperation;
  6. import android.content.ContentProviderResult;
  7. import android.content.ContentResolver;
  8. import android.content.Context;
  9. import android.database.Cursor;
  10. import android.net.Uri;
  11. import android.provider.ContactsContract;
  12. import android.provider.ContactsContract.Contacts;
  13. import android.provider.ContactsContract.Data;
  14. import android.provider.ContactsContract.Groups;
  15. import android.provider.ContactsContract.RawContacts;
  16. import android.util.Log;
  17. /**
  18. * 联系人管理,三个表 ContactsContract.Data.CONTENT_URI;
  19. * ContactsContract.RawContacts.CON9TENT_URI
  20. * ContactsContract.Contacts.CONTENT_URI
  21. *
  22. * @author liuyuqi
  23. *
  24. */
  25. public class ContactManager {
  26. ContentResolver resolver;
  27. static String TAG = "me.yoqi.deleteallcontacts.utils.ContactManager";
  28. public ContactManager(Context context) {
  29. resolver = context.getContentResolver();
  30. }
  31. /**
  32. * 删除所有联系人
  33. *
  34. * @return
  35. */
  36. public HashMap<String, Object> delAllContacts() {
  37. ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
  38. ContentProviderOperation op = null;
  39. Uri uri = null;
  40. HashMap<String, Object> delResult = new HashMap<String, Object>();
  41. int num = 0;// 删除影响的行数
  42. resolver.delete(
  43. Uri.parse(ContactsContract.RawContacts.CONTENT_URI.toString() + "?"
  44. + ContactsContract.CALLER_IS_SYNCADAPTER + "=true"),
  45. ContactsContract.RawContacts._ID + ">0", null);
  46. // 删除Data表的数据
  47. uri = Uri.parse(Data.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
  48. op = ContentProviderOperation.newDelete(uri).withSelection(Data.RAW_CONTACT_ID + ">0", null)
  49. .withYieldAllowed(true).build();
  50. ops.add(op);
  51. // 删除RawContacts表的数据
  52. uri = Uri.parse(RawContacts.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
  53. op = ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts._ID + ">0", null)
  54. .withYieldAllowed(true).build();
  55. ops.add(op);
  56. // 删除Contacts表的数据
  57. uri = Uri.parse(Contacts.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
  58. op = ContentProviderOperation.newDelete(uri).withSelection(Contacts._ID + ">0", null).withYieldAllowed(true)
  59. .build();
  60. ops.add(op);
  61. // 执行批量删除
  62. try {
  63. ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);
  64. for (ContentProviderResult result : results) {
  65. num += result.count;
  66. Log.i(TAG, "删除影响的行数:" + result.count);
  67. }
  68. delResult.put("result", "1");
  69. delResult.put("obj", num);
  70. } catch (Exception e) {
  71. Log.i(TAG, e.getMessage());
  72. delResult.put("result", "-1");
  73. delResult.put("obj", "删除失败!" + e.getMessage());
  74. }
  75. if (delResult.size() == 0) {
  76. delResult.put("result", "0");
  77. delResult.put("obj", "无效删除,联系人信息不正确!");
  78. }
  79. return delResult;
  80. }
  81. /**
  82. * 删除联系人 ,联系人相关联的有三张表,将三张表的数据全部删除 部分手机删除第二张即可,部分手机需要删除三张表
  83. * resolver.delete(ContactsContract.Data.CONTENT_URI, null, null);
  84. * resolver.delete(ContactsContract.RawContacts.CONTENT_URI, null, null);
  85. * resolver.delete(ContactsContract.Contacts.CONTENT_URI, null, null);
  86. *
  87. * @param contactId
  88. * 联系人ID
  89. * @param groupId
  90. * @return 如果删除成功:(1, num); 如果删除失败:(-1, "删除失败:" + e.getMessage());
  91. * 如果信息无效:(0, "无效删除,联系人信息不正确!");
  92. */
  93. public HashMap<String, Object> delContacts(List<String> contactIds) {
  94. ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
  95. ContentProviderOperation op = null;
  96. Uri uri = null;
  97. HashMap<String, Object> delResult = new HashMap<String, Object>();
  98. int num = 0;// 删除影响的行数
  99. if (Utils.isEmpty(contactIds)) {
  100. delResult.put("result", "0");
  101. delResult.put("obj", "无效删除,联系人id不正确!");
  102. return delResult;
  103. }
  104. List<String> ids = new ArrayList<String>();
  105. String selection = " in(";
  106. for (String contactId : contactIds) {
  107. // 如果联系人id 不大于 0,或者不存在,循环下一次
  108. if (!Utils.isNumber(contactId) || Long.parseLong(contactId) <= 0 || !isExistContact(contactId)) {
  109. continue;
  110. }
  111. selection += "?,";
  112. ids.add(contactId);
  113. }
  114. if (ids.size() == 0) {
  115. delResult.put("result", "0");
  116. delResult.put("obj", "无效联系人id");
  117. return delResult;
  118. }
  119. selection = selection.substring(0, selection.length() - 1) + ")";
  120. String[] selectionArgs = ids.toArray(new String[] {});
  121. // 删除Data表的数据
  122. uri = Uri.parse(Data.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
  123. op = ContentProviderOperation.newDelete(uri).withSelection(Data.RAW_CONTACT_ID + selection, selectionArgs)
  124. .withYieldAllowed(true).build();
  125. ops.add(op);
  126. // 删除RawContacts表的数据
  127. uri = Uri.parse(RawContacts.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
  128. op = ContentProviderOperation.newDelete(RawContacts.CONTENT_URI)
  129. .withSelection(RawContacts._ID + selection, selectionArgs).withYieldAllowed(true).build();
  130. ops.add(op);
  131. // 删除Contacts表的数据
  132. uri = Uri.parse(Contacts.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
  133. op = ContentProviderOperation.newDelete(uri).withSelection(Contacts._ID + selection, selectionArgs)
  134. .withYieldAllowed(true).build();
  135. ops.add(op);
  136. // 执行批量删除
  137. try {
  138. ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);
  139. for (ContentProviderResult result : results) {
  140. num += result.count;
  141. Log.i(TAG, "删除影响的行数:" + result.count);
  142. }
  143. delResult.put("result", "1");
  144. delResult.put("obj", num);
  145. } catch (Exception e) {
  146. Log.i(TAG, e.getMessage());
  147. delResult.put("result", "-1");
  148. delResult.put("obj", "删除失败!" + e.getMessage());
  149. }
  150. if (delResult.size() == 0) {
  151. delResult.put("result", "0");
  152. delResult.put("obj", "无效删除,联系人id不正确!");
  153. }
  154. return delResult;
  155. }
  156. /**
  157. * 根据id查询联系人是否存在
  158. */
  159. private boolean isExistContact(String id) {
  160. if (Utils.isEmpty(id)) {
  161. return false;
  162. }
  163. Cursor cursor = resolver.query(Contacts.CONTENT_URI, new String[] { Contacts._ID }, Contacts._ID + " = ? ",
  164. new String[] { id }, null);
  165. if (cursor.moveToFirst()) {
  166. return true;
  167. }
  168. cursor.close();
  169. return false;
  170. }
  171. /**
  172. * 根据组的 id 查询组是否存在
  173. *
  174. * @return boolean
  175. */
  176. public boolean isExistGroup(String id) {
  177. if (Utils.isEmpty(id)) {
  178. return false;
  179. }
  180. Cursor cursor = resolver.query(Groups.CONTENT_URI, new String[] { Groups._ID }, Groups._ID + "=" + id, null,
  181. null);
  182. if (cursor.moveToFirst()) {
  183. return true;
  184. }
  185. cursor.close();
  186. return false;
  187. }
  188. /**
  189. * 查询所有联系人
  190. */
  191. public ArrayList<String> getAllContact() {
  192. ArrayList<String> res = new ArrayList<String>();
  193. Uri uri = ContactsContract.Data.CONTENT_URI;
  194. Cursor cursorUser = resolver.query(uri,
  195. new String[] { ContactsContract.CommonDataKinds.Phone._ID,
  196. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  197. ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID,
  198. ContactsContract.CommonDataKinds.Phone.NUMBER,
  199. },
  200. null, null, null);
  201. while (cursorUser.moveToNext()) {
  202. int id = cursorUser.getInt(0); // 按上面数组的声明顺序获取
  203. String name = cursorUser.getString(1);
  204. int rawContactsId = cursorUser.getInt(2);
  205. String num = cursorUser.getString(3);
  206. res.add("id:" + id + " ,name:" + name + " ,rawContacntsID:" + rawContactsId +" 手机号:"+num+ "\r\n");
  207. }
  208. return res;
  209. }
  210. }