有需要的直接拿去用,
方法:
/*
*作者:赵星海
*时间: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();
}
如遇到问题 欢迎评论和指正,谢谢