首先需要引入libphonenumber-7.2.2 carrier-1.9 geocoder-2.32 prefixmapper-2.32 相关库地址
添加下面类 用户检测国家
import android.content.Context; import android.location.LocationManager; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; import java.util.Locale; 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; } }添加下面的类 用于获取号码相关信息
import android.content.Context; import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber; import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder; import java.util.Locale; 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(); } // 获取国家码 “86” public static int getCurrentCountryIsoNumber(Context context) { // The {@link CountryDetector} should never return null so this is safe to return as-is. return mPhoneNumberUtil.getCountryCodeForRegion(CountryDetector.getInstance(context).getCurrentCountryIso()); } /** * 根据电话号码获取国家代码 * * @param context * @param phoneNumber * @return */ public static int getCountryCode(Context context, String phoneNumber) { Phonenumber.PhoneNumber structuredNumber = getStructedNumber(context, phoneNumber); if (structuredNumber != null) { return structuredNumber.getCountryCode(); } return 0; } //获取归属地信息 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) { if (getStructedNumber(context, phoneNumber)!=null) { return mPhoneNumberUtil.isValidNumber(getStructedNumber(context, phoneNumber)); } return false; } 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); } /** * 根据国家代码和手机号判断归属地 * * @param context * @param phoneNumber * @param countryCode * @return */ public static String getGeo(Context context, String phoneNumber, int countryCode) { int ccode = countryCode; long phone = Long.parseLong(phoneNumber); Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber(); pn.setCountryCode(ccode); pn.setNationalNumber(phone); PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance(); Locale locale = context.getResources().getConfiguration().locale; return geocoder.getDescriptionForNumber(pn, locale); } }