安卓获取电话本数据并且获取同一个联系人下多个号码

ContactInfo 是自己封装的实体对象可以按自己的需求进行封装

思路:通过数据库查询所有联系人 获取到数据库唯一标识ID,获取到联系人名称  查询联系人号码是否为空如果不为空

查询当前ID号下面有多少电话号码 全部装入到list中 需要在做联系人号码类型的可以在这里进行判断我这边没有这个需求只需要知道这个联系人有多少个号码就行了,在两个循环结束后 添加联系人名称添加联系人多号码列表

 /**
     * 获取联系人列表
     *
     * @return 联系人列表
     */
    private Cursor contactcursor;

    /**
     * 查询电话本数据
     *
     * @return
     */
    public void getContact() {
        Log.d(TAG, "getContact() 电话本查询");
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        contactcursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
        if (null != contactcursor && contactcursor.getCount() > 0) {
            while (contactcursor.moveToNext()) {
                ContactInfo contactInfo = new ContactInfo();
                ArrayList<String> allNumber = contactInfo.getAllNumber();
                //联系人ID
                String id = contactcursor.getString(contactcursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name = contactcursor.getString(contactcursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                int numberCount = contactcursor.getInt(contactcursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                Log.d(TAG, "nuberCount:" + numberCount);
                //查询该对象是否有多个号码
                if (numberCount > 0) {
                    Cursor phoneNumberCursor =resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id}, null);
                    while (phoneNumberCursor.moveToNext()) {
                        String phoneNumber = phoneNumberCursor.getString(phoneNumberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        if (allNumber == null) {
                            allNumber = new ArrayList<>();
                        }
                        if (!allNumber.contains(phoneNumber)) {
                            allNumber.add(phoneNumber);
                            contactInfo.setAllNumber(allNumber);
                        }

                    }
                    if (phoneNumberCursor != null) {
                        phoneNumberCursor.close();
                    }
                }
                contactInfo.setName(name);//添加名字
                contactInfo.setAllNumber(allNumber);//所有号码
                phoneContactInfos.add(contactInfo);
          
            }

        }
        if (contactcursor != null) {
            contactcursor.close();
        }
        LogUtils.d(TAG, "电话本下载数据" + phoneContactInfos.size());
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值