Android 快速获取联系人列表

1.首先要获取相应权限

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

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

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


注意:6.0以上要动态获取权限

2.获取到相应权限后  开始快速读取本地联系人列表 (handler + Thread 结合使用读取联系人列表 并通过回调回去)
 

    public void getContactList(final BaseDataSource.GetDataSourceCallback<List<ContactsInfo>> callback) {
     
        ActivityUtil.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ContactAsyncQueryHandler asyncQueryHandler = new ContactAsyncQueryHandler(CCApplication.getInstance().getContentResolver(), callback);
                Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
                String[] projection = {
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                        ContactsContract.CommonDataKinds.Phone._ID,
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.DATA1,
                        ContactsContract.CommonDataKinds.Phone.STARRED,
                        ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI,
                        "sort_key"};
                asyncQueryHandler.startQuery(0, null, uri, projection, null, null, "sort_key COLLATE LOCALIZED asc");
            }
        });
    }
     
    private static class ContactAsyncQueryHandler extends AsyncQueryHandler {
     
        private BaseDataSource.GetDataSourceCallback<List<ContactsInfo>> mCallback;
     
        public ContactAsyncQueryHandler(ContentResolver cr, BaseDataSource.GetDataSourceCallback<List<ContactsInfo>> callback) {
            super(cr);
            this.mCallback = callback;
        }
     
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            Map<Integer, ContactsInfo> contactIdMap;
            List<ContactsInfo> contactsInfos = new ArrayList<ContactsInfo>();
            if (cursor != null && cursor.getCount() > 0) {
                contactIdMap = new HashMap<>();
                cursor.moveToFirst();
                for (int i = 0; i < cursor.getCount(); i++) {
                    cursor.moveToPosition(i);
                    int contactId = cursor.getInt(0);
                    int id = cursor.getInt(1);
                    String name = cursor.getString(2);
                    String number = cursor.getString(3);
                    int isStarted = cursor.getInt(4);
                    String photo = cursor.getString(5);
     
                    if (!contactIdMap.containsKey(contactId) && name != null && number != null) {
                        ContactsInfo contactsInfo = new ContactsInfo();
                        contactsInfo.setContactId(contactId);
                        contactsInfo.setName(name);
                        contactsInfo.setPhone(number);
                        contactsInfo.setPhoto(photo);
                        contactsInfo.setStared(isStarted == 1);
                        contactsInfos.add(contactsInfo);
                        contactIdMap.put(contactId, contactsInfo);
                    }
                }
                cursor.close();
            }
            mCallback.onLoaded(contactsInfos);
        }
    }
---------------------  
作者:徐朵朵的小太阳  
来源:CSDN  
原文:https://blog.csdn.net/GodnessIsMyMine/article/details/83785794  
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值