# Android系统联系人 **Repository Path**: coolxinxins/android_system_contacts ## Basic Information - **Project Name**: Android系统联系人 - **Description**: 对Android系统联系人的CRUD操作 - **Primary Language**: Android - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2018-12-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Android系统联系人 #### 介绍 对Android系统联系人的CRUD操作 #### 使用说明 放在service中操作是最好的,但是试过系统监听联系人变化的onChange方法并不是很好用,有时候会调用多次直接导致ANR 1.这个查询的是在显示界面的字段 剩下的字段是在联系人的详情界面去查询,这样的好处是每次都只需要去查询一个人不用查询全部 查询一个联系人剩下的那些信息还是非常快的 private void updateContactsInfo() { startTime = System.currentTimeMillis(); Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 联系人Uri; // 查询的字段 String[] phoneProjection = { ContactsContract.CommonDataKinds.Phone.CONTACT_ID,//联系人ID ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,//姓名 ContactsContract.CommonDataKinds.Phone.DATA,//具体数据项 PHONE_BOOK_LABLE,//首字母 ContactsContract.CommonDataKinds.Phone.PHOTO_ID//图像ID }; // 按照sort_key升序查詢 mQueryHandler.startQuery(0, null, phoneUri, phoneProjection, null, null, ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY); } public class MyAsyncQueryHandler extends AsyncQueryHandler { public MyAsyncQueryHandler(ContentResolver resolver) { super(resolver); } @Override protected void onQueryComplete(int token, Object cookie, Cursor cursor) { super.onQueryComplete(token, cookie, cursor); mContactList.clear(); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); int contactId = cursor.getInt(0); int rawId = cursor.getInt(1); String name = cursor.getString(2); String number = cursor.getString(3); String sortKey = cursor.getString(4); long photoId = cursor.getLong(5); // 创建联系人对象 SortModel contact = new SortModel(); contact.setSort_id(contactId); contact.setRawId(rawId); contact.setName(name); contact.setNumber(number); contact.setSortKey(sortKey); contact.setPhotoId(photoId); contact.setSortLetters(sortKey); mContactList.add(contact); if (photoId != 0) { Bitmap bitmap = ContactsTools.getPhoto(contactId, MainActivity.this); contact.setBtHead(bitmap); } } } adapter.notifyDataSetChanged(); adapter.updateListView(mContactList); Log.d(TAG, "loadContacts time = " + (System.currentTimeMillis() - startTime)); } } 2. 将我们在主界面查询到的RawId传到联系人详情界面,以便查询剩下的所有信息 mContactId = getIntent().getIntExtra("raw_contact_id", 0); if (mContactId > 0) { //根据raw_contact_id查询data数据库表,得到联系人邮箱和地址 ContentResolver contentResolver = this.getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Email.DATA}, ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(mContactId)}, null); while (cursor.moveToNext()) { String email = cursor.getString(0); tvEmail.setText(email); MyApplication.sortModel.setEmail(email); } cursor.close(); Cursor cursor1 = contentResolver.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.StructuredPostal.STREET, ContactsContract.CommonDataKinds.StructuredPostal.POBOX, ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD, ContactsContract.CommonDataKinds.StructuredPostal.CITY, ContactsContract.CommonDataKinds.StructuredPostal.REGION, ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE}, ContactsContract.CommonDataKinds.StructuredPostal.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(mContactId)}, null); while (cursor1.moveToNext()) { String street = cursor1.getString(0); String pobx = cursor1.getString(1); String community = cursor1.getString(2); String city = cursor1.getString(3); String region = cursor1.getString(4); String postCode = cursor1.getString(5); StringBuilder sb = new StringBuilder(); if (street != null) { sb.append(street); } if (pobx != null) { sb.append(pobx); } if (community != null) { sb.append(community); } if (city != null) { sb.append(city); } if (region != null) { sb.append(region); } if (postCode != null) { sb.append(postCode); } tvAddress.setText(sb.toString()); MyApplication.sortModel.setAddress(sb.toString()); } cursor1.close(); } 3. xxxx 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 码云特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)