android 查询通讯录数据

2019-01-16  本文已影响1人  勤劳的蚂蚁
 new Thread() {
            @Override
            public void run() {
                printContacts();
                handler.sendEmptyMessage(1);

            }
        }.start();

     /*
    * 自定义显示Contacts提供的联系人的方法
    */
    private void printContacts() {

       
        Cursor cursor = null;
        Cursor phoneCursor = null;
        try {
            //生成ContentResolver对象
            ContentResolver contentResolver = getContentResolver();
            // 获得所有的联系人
        /*Cursor cursor = contentResolver.query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
         */
            //这段代码和上面代码是等价的,使用两种方式获得联系人的Uri
            cursor = contentResolver.query(Uri.parse("content://com.android.contacts/contacts"), null, null, null, null);
            CustomInfoBean customInfoBean = null;
            // 循环遍历
            if (cursor.moveToFirst()) {
                int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                int displayNameColumn = cursor
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                do {
                    // 获得联系人的ID
                    String contactId = cursor.getString(idColumn);
                    // 获得联系人姓名
                    String displayName = cursor.getString(displayNameColumn);
                    if (TextUtils.isEmpty(displayName)) {/*姓名为空 跳过 继续查找*/
                        continue;
                    }
                    if (containsEmoji(displayName)) {/*姓名包含 emoji 跳过 继续查找*/
                        continue;
                    }
              
                    /*设置联系人名字*/
                   todo  拿到数据处理
                    // 查看联系人有多少个号码,如果没有号码,返回0
                    int phoneCount = cursor
                            .getInt(cursor
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    if (phoneCount > 0) {
                        // 获得联系人的电话号码列表
                        phoneCursor = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + "=" + contactId, null, null);
                        /* 定义一个数 来获取不同的 phone*/
                        StringBuilder sb = new StringBuilder();
//                        /*计数*/
                        /*限制导入 5个以内*/
                        int i = 0;
                        if (phoneCursor.moveToFirst()) {
                            do {

                                //遍历所有的联系人下面所有的电话号码
                                String phoneNum = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                if (!TextUtils.isEmpty(phoneNum) && phoneNum.length() < 20) {
                                    /*限制 导入 5个 电话号码*/
                                    i++;
                                    if (i >= 6) {
                                        continue;
                                    }
                                    // 20170929过滤 包含+86 和 "-" 的字段  将其替换为 "" 空串
                                    phoneNum = phoneNum.replace("+86", "").replace("-", "");
//                                    if(i<phoneCount){
                                    sb.append(phoneNum).append(",");
//                                    }else if(i==phoneCount){
//                                        sb.append(phoneNum);
//                                    }
                                } else {
                                    continue;
                                }
                            } while (phoneCursor.moveToNext());
                        }
                        String mb = getMobils(sb.toString());
                        if (TextUtils.isEmpty(mb)) {/*电话为空 跳过 继续查找*/
                            continue;
                        }
                   todo  设置电话号码
                        if (phoneCursor != null) {
                            phoneCursor.close();
                        }
                    } else {
                        continue;
                    }
                      /*每次添加 内容*/
                    if (bean!= null) {
                        listCus.add(bean);
                        bean= null;
                    }
                } while (cursor.moveToNext());


            }
            //List数据去重复
            if (listCus != null && listCus.size() > 0) {
                ListUtils.removeDuplicate(listCus);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (phoneCursor != null) {
                phoneCursor.close();
            }
        }
    }


 /**
     * 对电话号码去空格和 去 “—”,最后没有 “,”
     *
     * @param mobils
     * @return
     */
    public static String getMobils(String mobils) {
        String dest = "";
        if (mobils != null) {
     /* 去空格*/
            dest = mobils.replaceAll(" ", "");
        /*去"-"*/
            dest = dest.replaceAll("-", "");
        /*去掉最后 已 ,结尾*/
            if (dest.endsWith(",")) {
                dest = dest.substring(0, dest.length() - 1);
            }
        }
        return dest;
    }
上一篇 下一篇

猜你喜欢

热点阅读