目标:获取手机机主手机号码
(注:我只给出了代码思路和用法示例,大家可以自己调用方法debug运行看结果或者自行编写显示结果的代码,根据需要进行修改,希望对您有帮助!)
eg:
- public class ContactUtils {
- private static TelephonyManager telephonyManager;
-
- /**
- * 获取本地SIM卡手机机主号码
- *
- * @return
- */
- public static String getNativePhoneNumber(Context cxt) {
- telephonyManager = (TelephonyManager) cxt
- .getSystemService(Context.TELEPHONY_SERVICE);
- String NativePhoneNumber = null;
- NativePhoneNumber = telephonyManager.getLine1Number();
- return NativePhoneNumber;
- }
- }
目标:获取手机通讯录联系人的姓名和手机号码,屏幕座机和其他非手机号码,避免号码重复
(注:我只给出了代码思路和用法示例,大家可以自己调用方法debug运行看结果或者自行编写显示结果的代码,根据需要进行修改,希望对您有帮助!)
eg:
- /**
- * 获取手机通讯录
- * 只获取正确非重复手机号码
- * @author 诺诺
- *
- */
- public class ContactUtils {
- private static final String TAG = "ContactList";
-
- // 获取系统数据库联系人Phone表字段信息
- private static final String[] phoneContact = new String[] {
- Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID };
- // 联系人的ID
- private static final int contactID_Contact = 0;
- // 联系人名称
- private static final int name_Contact = 1;
- // 电话号码
- private static final int num_Contact = 2;
- // 头像ID
- private static final int phoneID_Contact = 3;
-
- /**
- * 获取手机联系人信息:只获取正确非重复手机号
- * @param context
- * @return ArrayList
- */
- public static ArrayList<ContactInfo> getContactsList(Context context) {
-
- ContentResolver resolver = context.getContentResolver();
- Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, phoneContact,
- null, null, ContactsContract.Contacts.DISPLAY_NAME
- + " COLLATE LOCALIZED ASC");
-
- // <name,ContactInfo>
- HashMap<String, ContactInfo> nameMap = new HashMap<String, ContactInfo>();
- // 通讯录的所有手机号码
- ArrayList<String> phoneList = new ArrayList<String>();
-
- if (phoneCursor != null) {
- //通讯录所有联系人信息
- ArrayList<ContactInfo> contacts = new ArrayList<ContactInfo>();
-
- ContactInfo contactInfo = null;
- while (phoneCursor.moveToNext()) {
- // 得到手机号码
- String phoneNumber = phoneCursor.getString(num_Contact);
- Log.i(TAG + " phoneNumber: ", phoneNumber);
-
- // 当手机号码为空的或非正确手机号 跳过此次循环
- if (TextUtils.isEmpty(phoneNumber)
- || !RegexUtils.isMoblieNo(phoneNumber)){
- continue;
- }
- // 得到联系人名称
- String contactName = phoneCursor.getString(name_Contact);
- // 得到联系人ID
- int contactid = phoneCursor.getInt(contactID_Contact);
- // 得到联系人头像ID
- Long photoid = phoneCursor.getLong(phoneID_Contact);
-
- if (phoneList.contains(phoneNumber)) {
- continue;
- } else {
- phoneList.add(phoneNumber);
- if (!nameMap.containsKey(contactName)) {
- contactInfo = new ContactInfo();
- contactInfo.name = contactName;
- contactInfo.mobilePhoneList.add(phoneNumber);
- contacts.add(contactInfo);
- nameMap.put(contactName, contactInfo);
- } else {
- contactInfo = nameMap.get(contactName);
- contactInfo.mobilePhoneList.add(phoneNumber);
- }
- }
- }
- phoneCursor.close();
- return contacts;
- }
- return null;
- }
-
- /**
- * 联系人信息类
- */
- public static class ContactInfo implements Serializable {
- public String name; // 联系人姓名
- public ArrayList<String> mobilePhoneList; // 手机号码
-
- public ContactInfo() {
- CheckNullPointer();
- }
-
- /**
- * 避免发生空指针异常
- */
- public void CheckNullPointer() {
- if (mobilePhoneList == null)
- mobilePhoneList = new ArrayList<String>();
-
- if (name == null)
- name = "";
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- public ArrayList<String> getPhoneNumList() {
- return mobilePhoneList;
- }
-
- public void setPhoneNumList(ArrayList<String> mobilePhone) {
- mobilePhoneList = mobilePhone;
- }
- }
- }
- public class RegexUtils {
- /**
- * 验证是否是有效手机号
- *
- * 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
- *
- * 联通:130、131、132、152、155、156、185、186
- *
- * 电信:133、153、180、189、(1349卫通)
- *
- * @param mobiles
- * @return
- */
- public static boolean isMoblieNo(String mobiles) {
- Pattern p = Pattern
- .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
- Matcher m = p.matcher(mobiles);
-
- return m.matches();
- }
- }