package com.szzc.ucar.utils;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.szzc.ucar.application.PilotApp;
import com.szzc.ucar.pilot.mode.PassengerEntry;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
public class ContactsUtils {
public static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER };
public static ContentResolver resolver = PilotApp.getInstance()
.getContentResolver();
public static final int PHONES_DISPLAY_NAME_INDEX = 0;
public static final int PHONES_NUMBER_INDEX = 1;
public static ArrayList<PassengerEntry> getPhoneContracts() {
ArrayList<PassengerEntry> list = new ArrayList<PassengerEntry>();
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
PassengerEntry passenger = new PassengerEntry();
passenger.phone = phoneCursor.getString(PHONES_NUMBER_INDEX);
if (TextUtils.isEmpty(passenger.phone))
continue;
if (checkPhoneNumeberCountryCode(passenger.phone)) {
passenger.countryCode = "86";
} else {
passenger.countryCode = "";
}
passenger.phone = trimPhoneNumber(passenger.phone);
passenger.name = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
list.add(passenger);
}
phoneCursor.close();
}
return list;
}
public static ArrayList<PassengerEntry> getSimContacts() {
ArrayList<PassengerEntry> list = new ArrayList<PassengerEntry>();
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
PassengerEntry passenger = new PassengerEntry();
passenger.phone = phoneCursor.getString(PHONES_NUMBER_INDEX);
if (TextUtils.isEmpty(passenger.phone))
continue;
if (checkPhoneNumeberCountryCode(passenger.phone)) {
passenger.countryCode = "86";
} else {
passenger.countryCode = "";
}
passenger.phone = trimPhoneNumber(passenger.phone);
passenger.name = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
list.add(passenger);
}
phoneCursor.close();
}
return list;
}
// 判断手机号是不是+86开头
public static boolean checkPhoneNumeberCountryCode(String phone) {
if (!TextUtils.isEmpty(phone) && phone.startsWith("+86")) {
return true;
}
return false;
}
public static String trimPhoneNumber(String phone) {
phone = phone.trim();
if (phone.contains(" ")) {
phone = phone.replaceAll(" ", "");
}
if (phone.contains("-")) {
phone = phone.replaceAll("-", "");
}
if (phone.startsWith("+86")) {
phone = phone.substring(3);
}
return phone;
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.szzc.ucar.application.PilotApp;
import com.szzc.ucar.pilot.mode.PassengerEntry;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
public class ContactsUtils {
public static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER };
public static ContentResolver resolver = PilotApp.getInstance()
.getContentResolver();
public static final int PHONES_DISPLAY_NAME_INDEX = 0;
public static final int PHONES_NUMBER_INDEX = 1;
public static ArrayList<PassengerEntry> getPhoneContracts() {
ArrayList<PassengerEntry> list = new ArrayList<PassengerEntry>();
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
PassengerEntry passenger = new PassengerEntry();
passenger.phone = phoneCursor.getString(PHONES_NUMBER_INDEX);
if (TextUtils.isEmpty(passenger.phone))
continue;
if (checkPhoneNumeberCountryCode(passenger.phone)) {
passenger.countryCode = "86";
} else {
passenger.countryCode = "";
}
passenger.phone = trimPhoneNumber(passenger.phone);
passenger.name = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
list.add(passenger);
}
phoneCursor.close();
}
return list;
}
public static ArrayList<PassengerEntry> getSimContacts() {
ArrayList<PassengerEntry> list = new ArrayList<PassengerEntry>();
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
PassengerEntry passenger = new PassengerEntry();
passenger.phone = phoneCursor.getString(PHONES_NUMBER_INDEX);
if (TextUtils.isEmpty(passenger.phone))
continue;
if (checkPhoneNumeberCountryCode(passenger.phone)) {
passenger.countryCode = "86";
} else {
passenger.countryCode = "";
}
passenger.phone = trimPhoneNumber(passenger.phone);
passenger.name = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
list.add(passenger);
}
phoneCursor.close();
}
return list;
}
// 判断手机号是不是+86开头
public static boolean checkPhoneNumeberCountryCode(String phone) {
if (!TextUtils.isEmpty(phone) && phone.startsWith("+86")) {
return true;
}
return false;
}
public static String trimPhoneNumber(String phone) {
phone = phone.trim();
if (phone.contains(" ")) {
phone = phone.replaceAll(" ", "");
}
if (phone.contains("-")) {
phone = phone.replaceAll("-", "");
}
if (phone.startsWith("+86")) {
phone = phone.substring(3);
}
return phone;
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
别忘了权限
需要的权限 <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>