1. 程式人生 > >Android 一個方法,拿到手機聯絡人(附:手機號正則判斷)

Android 一個方法,拿到手機聯絡人(附:手機號正則判斷)

有需要的直接拿去用,

方法:

 /*
     *作者:趙星海
     *時間:18/06/28 16:40
     *用途: 獲取手機聯絡人
     */
    private ArrayList<Bean_Contacts> getContacts() {
        ArrayList<Bean_Contacts> list = new ArrayList<>();

        //聯絡人的Uri,也就是content://com.android.contacts/contacts
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        //指定獲取_id和display_name兩列資料,display_name即為姓名
        String[] projection = new String[]{
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        //根據Uri查詢相應的ContentProvider,cursor為獲取到的資料集
        Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Long id = cursor.getLong(0);
                //獲取姓名
                String name = cursor.getString(1);
                //指定獲取NUMBER這一列資料
                String[] phoneProjection = new String[]{
                        ContactsContract.CommonDataKinds.Phone.NUMBER
                };
                Bean_Contacts bean = new Bean_Contacts();
                bean.setName(name);
                //根據聯絡人的ID獲取此人的電話號碼
                Cursor phonesCusor = this.getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        phoneProjection,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,
                        null,
                        null);

                //因為每個聯絡人可能有多個電話號碼,所以需要遍歷
                if (phonesCusor != null && phonesCusor.moveToFirst()) {
                    do {
                        String Phone = phonesCusor.getString(0);
                        String Phone1 = Phone.replace(" ", "");//去掉所有空格,包括首尾、中間

                        bean.setPhone(Phone1); // 手機號
                    } while (phonesCusor.moveToNext());
                }
                if (GeneralUtils.isChinaPhoneLegal(bean.getPhone())){   //判斷手機號是否符合規則
                    list.add(bean);
                }
            } while (cursor.moveToNext());

        }
        return list;
    }

Bean物件:

/**
 * Created by Xinghai.Zhao on 18/06/28.
 */

public class Bean_Contacts {
    @Override
    public String toString() {
        return "Bean_Contacts{" +
                "phone='" + phone + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    private String phone;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;

}

手機號正則判斷方法:

/*
     *作者:趙星海
     *時間:18/07/04 10:32
     *用途:  手機號判斷   true通過驗證
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        if (str == null) {
            return false;
        }
        if (str.length() != 11) {
            return false;
        }
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

如遇到問題 歡迎評論和指正,謝謝