public static void fetchContact(Context context) {    List 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 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 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 classList = new ArrayList<>();        list.clear();        classList.addAll(hs);        saveToRealm(classList);        Log.e("resize", classList.size() + "");    } catch (Exception e) {    }*/    }}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android in linux 笔记
  2. Android短视频开发
  3. Android禁止横屏和竖屏切换
  4. Android RadioButton背景图片和文字的相
  5. android中SQLite的使用总结,用excSQL和raw
  6. iOS程序员必须知道的Android要点
  7. Androidmanifest.xml里的
  8. android每日一问1【2011-8-29】
  9. Android获取keystore文件的信息
  10. Android(安卓)更新获取 请求头 Rxjava+Re