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) { }*/ } }
android 获取通讯录中同一联系人的多个号码
最新推荐文章于 2021-10-18 14:41:33 发布