Android 获取通讯录

添加权限

<!-- 读联系人权限 -->  
<uses-permission android:name="android.permission.READ_CONTACTS" />  
<!-- 写联系人权限 -->  
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
<!-- 拨号权限 -->  
<uses-permission android:name="android.permission.CALL_PHONE" />  
<!-- 读短信权限 -->  
<uses-permission android:name="android.permission.READ_SMS" />  

自定义一个AsyncQueryHandler的类

 private class MyAsyncQueryHandler extends AsyncQueryHandler {
        ...
        }

实现 onQueryComplete(int token, Object cookie, Cursor cursor)
其中主要就是游标Cursor,通讯录就放在游标中,需要遍历游标来获取到联系人

private Map<Integer, Contact> contactIdMap;
private List<Contact> mList;



 @Override
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
        if (cursor != null && cursor.getCount() > 0) {
            contactIdMap = new HashMap<>();
            mList = new ArrayList<>();
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToPosition(i);
                String name = cursor.getString(1);
                String number = cursor.getString(2);
                String sortKey = cursor.getString(3);
                int contactId = cursor.getInt(4);
                Long photoId = cursor.getLong(5);
                String lookUpKey = cursor.getString(6);

                if (contactIdMap.containsKey(contactId)) {
                    // 无操作
                } else {
                    // 创建联系人对象
                    Contact contact = new Contact();
                    contact.setDesplayName(name);
                    contact.setPhoneNum(number);
                    contact.setSortKey(sortKey);
                    contact.setPhotoId(photoId);
                    contact.setLookUpKey(lookUpKey);
                    mList.add(contact);

                    contactIdMap.put(contactId, contact);
                }
            }

            // 遍历结束
            if (mList.size() > 0) {
                mContactAdapter.setContactList(mList);
            }
        }
        super.onQueryComplete(token, cookie, cursor);
    }
}

查询时调用如下

 Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 联系人Uri;
    // 查询的字段
    String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key",
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
            ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY };
// 按照sort_key升序查詢
    mAsyncQueryHandler.startQuery(0, null, uri, projection, null, null,
            "sort_key COLLATE LOCALIZED asc");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值