1 Star 3 Fork 0

coolxinxin / Android系统联系人

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
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));
    }
}
  1. 将我们在主界面查询到的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();
     }
  2. xxxx

  3. Fork 本仓库

  4. 新建 Feat_xxx 分支

  5. 提交代码

  6. 新建 Pull Request

码云特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. 码云官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
  4. GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
  5. 码云官方提供的使用手册 https://gitee.com/help
  6. 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/

空文件

简介

对Android系统联系人的CRUD操作 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/coolxinxins/android_system_contacts.git
git@gitee.com:coolxinxins/android_system_contacts.git
coolxinxins
android_system_contacts
Android系统联系人
master

搜索帮助