libphonenumber 是google 开源的库,提供手机号码格式化,来电归属地,运营商等多种功能十分强大,现在做个简单的demo
1、首先下载 libphonumber 相关的库 here, 下载 carrier-1.9.jar、geocoder-2.32.jar、libphonenumber-7.2.2.jar、/prefixmapper-2.32.jar 这4个库。放在源码目录 lib 文件夹下(本人用的是 AS), 右键--> add as library 。
2、接下来就是怎样使用 其中的api了,下面是geo 工具类
public class GeoUtil {
private static PhoneNumberUtil mPhoneNumberUtil = PhoneNumberUtil.getInstance();
private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
// 获取国家码 “CN”
public static String getCurrentCountryIso(Context context) {
// The {@link CountryDetector} should never return null so this is safe to return as-is.
return CountryDetector.getInstance(context).getCurrentCountryIso();
}
//获取归属地信息
public static String getGeocodedLocationFor(Context context,String phoneNumber) {
final PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
Phonenumber.PhoneNumber structuredNumber = getStructedNumber(context,phoneNumber);
Locale locale = context.getResources().getConfiguration().locale;
return geocoder.getDescriptionForNumber(structuredNumber,locale);
}
//检查是否为 有效号码
public static boolean checkPhoneNumber(Context context,String phoneNumber) {
return mPhoneNumberUtil.isValidNumber(getStructedNumber(context,phoneNumber));
}
public static Phonenumber.PhoneNumber getStructedNumber(Context context,String phoneNumber) {
try {
final Phonenumber.PhoneNumber structuredNumber =
mPhoneNumberUtil.parse(phoneNumber,getCurrentCountryIso(context));
return structuredNumber;
} catch (NumberParseException e) {
return null;
}
}
//获取运营商信息
public static String getCarrier(Context context,String phoneNumber) {
Phonenumber.PhoneNumber structedNumber = getStructedNumber(context,phoneNumber);
Locale locale = context.getResources().getConfiguration().locale;
return carrierMapper.getNameForNumber(structedNumber,Locale.US);
}
}
以下为获取国家码的工具类
public class CountryDetector {
private static final String TAG = CountryDetector.class.getSimpleName();
private static CountryDetector sInstance;
private final Context mContext;
private final TelephonyManager mTelephonyManager;
private final LocationManager mLocationManager;
private final LocaleProvider mLocaleProvider;
private final String DEFAULT_COUNTRY_ISO = "CN";
public static class LocaleProvider {
public Locale getDefaultLocale() {
return Locale.getDefault();
}
}
private CountryDetector(Context context) {
this(context,(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE),
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE),
new LocaleProvider());
}
private CountryDetector(Context context, TelephonyManager telephonyManager,
LocationManager locationManager, LocaleProvider localeProvider) {
mTelephonyManager = telephonyManager;
mLocationManager = locationManager;
mLocaleProvider = localeProvider;
mContext = context;
}
public static CountryDetector getInstance(Context context) {
if(sInstance == null) {
sInstance = new CountryDetector(context);
}
return sInstance;
}
public String getCurrentCountryIso() {
String result = null;
if (isNetworkCountryCodeAvailable()) {
result = getNetworkBasedCountryIso();
Log.d(TAG," getNetworkBasedCountryIso");
}
if (TextUtils.isEmpty(result)) {
result = getSimBasedCountryIso();
Log.d(TAG,"getSimBasedCountryIso");
}
if (TextUtils.isEmpty(result)) {
result = getLocaleBasedCountryIso();
Log.d(TAG,"getLocaleBasedCountryIso");
}
if (TextUtils.isEmpty(result)) {
result = DEFAULT_COUNTRY_ISO;
Log.d(TAG,"DEFAULT_COUNTRY_ISO");
}
Log.d(TAG," result == " + result);
return result.toUpperCase(Locale.US);
}
/**
* @return the country code of the current telephony network the user is connected to.
*/
private String getNetworkBasedCountryIso() {
return mTelephonyManager.getNetworkCountryIso();
}
/**
* @return the country code of the SIM card currently inserted in the device.
*/
private String getSimBasedCountryIso() {
return mTelephonyManager.getSimCountryIso();
}
/**
* @return the country code of the user's currently selected locale.
*/
private String getLocaleBasedCountryIso() {
Locale defaultLocale = mLocaleProvider.getDefaultLocale();
if (defaultLocale != null) {
return defaultLocale.getCountry();
}
return null;
}
private boolean isNetworkCountryCodeAvailable() {
// On CDMA TelephonyManager.getNetworkCountryIso() just returns the SIM's country code.
// In this case, we want to ignore the value returned and fallback to location instead.
return mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM;
}
}
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
mEditNumber = (EditText) findViewById(R.id.number);
geocoder = PhoneNumberOfflineGeocoder.getInstance();
phoneNumberUtil = PhoneNumberUtil.getInstance();
}
public void submit(View v) {
phoneNumber = mEditNumber.getText().toString().trim();
boolean isValidnumber = GeoUtil.checkPhoneNumber(this,phoneNumber);
String carrier = GeoUtil.getCarrier(this,phoneNumber);
location = GeoUtil.getGeocodedLocationFor(this, phoneNumber);
Intent intent = new Intent(this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putBoolean("ifValidNumber",isValidnumber);
bundle.putString("location", location);
bundle.putString("carrier",carrier);
intent.putExtras(bundle);
startActivity(intent);
}
Github: https://github.com/googlei18n/libphonenumber
android source: http://androidxref.com/6.0.1_r10/xref/packages/apps/ContactsCommon/src/com/android/contacts/common/GeoUtil.java#34
网页版的demo:http://giggsey.com/libphonenumber/index.php