android 获取通讯录中同一联系人的多个号码

public static void fetchContact(Context context) {
    List<ContactsBean> list = new ArrayList<>();

    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            List<ContactsBean> numberlist = new ArrayList<>();
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            ContactsBean contactClass = new ContactsBean();
            contactClass.setId(id);
            contactClass.setName(name);
            if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {

                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    int type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    //   Log.e("called", phoneNo + "  " + name + "  " + id);
                    switch (type) {

                        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo + "  name   " + name + "  type  " + "home");
                                    numberlist.add(contactClass);
                                }


                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }


                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo + "  name   " + name + "  type  " + "mobile");
                                    numberlist.add(contactClass);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }


                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo + "  name   " + name + "  type  " + "work");
                                    numberlist.add(contactClass);
                                }


                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }

                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo + "  name   " + name + "  type  " + "eaxhome");
                                    numberlist.add(contactClass);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }

                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo + "  name   " + name + "  type  " + "faxwork");
                                    numberlist.add(contactClass);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }

                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo + "  name   " + name + "  type  " + "main");
                                    numberlist.add(contactClass);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }

                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                            try {

                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo + "  name   " + name + "  type  " + "home");
                                    numberlist.add(contactClass);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }

                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo);
                                    numberlist.add(contactClass);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }

                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
                            try {
                                if (phoneNo.equals("") || phoneNo != null) {
                                    contactClass.setPhoneNum(phoneNo);
                                    Log.e("test", phoneNo);
                                    numberlist.add(contactClass);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, e.toString());
                            }

                            break;
                    }
                }
                pCur.close();

           /* HashSet<ContactClass> h = new HashSet<>();
            h.addAll(numberlist);
            numberlist.clear();
            numberlist.addAll(h);*/
                contactClass.setPhoneNumList(numberlist);
                list.add(contactClass);
                for (int i = 0; i < numberlist.size(); i++) {
                    Log.e("inner", numberlist.get(i).getName() + " number  " + numberlist.get(i).getPhoneNum());
                }

            }

        }

   /* try {
        HashSet hs = new HashSet();

        hs.addAll(list); // demoArrayList= name of arrayList from which u want to remove duplicates
        List<ContactClass> classList = new ArrayList<>();
        list.clear();
        classList.addAll(hs);
        saveToRealm(classList);
        Log.e("resize", classList.size() + "");
    } catch (Exception e) {
    }*/
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值