首先需要引入libphonenumber-7.2.2 carrier-1.9 geocoder-2.32 prefixmapper-2.32 相关库地址
添加下面类 用户检测国家
import import android.location.LocationManager; import import 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 private CountryDetector(Context context) {this(context,(TelephonyManager) SystemService(Context.TELEPHONY_SERVICE),(LocationManager) SystemService(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 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 return null;}private boolean isNetworkCountryCodeAvailable() {// On NetworkCountryIso() 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 import import import import import import java.util.Locale;public class GeoUtil {private static PhoneNumberUtil mPhoneNumberUtil = Instance();private static PhoneNumberToCarrierMapper carrierMapper = Instance();// 获取国家码 “CN” public static String getCurrentCountryIso(Context context) {// The {@link CountryDetector} should never return null so this is safe to return as-is. return // 获取国家码 “86” public static int getCurrentCountryIsoNumber(Context context) {// The {@link CountryDetector} should never return null so this is safe to return as-is. return mPhoneNumberUtil.Instance(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 return 0;}//获取归属地信息 public static String getGeocodedLocationFor(Context context, String phoneNumber) {final PhoneNumberOfflineGeocoder geocoder = Instance();Phonenumber.PhoneNumber structuredNumber = getStructedNumber(context, phoneNumber);Locale locale = Resources().getConfiguration().locale;return //检查是否为 有效号码 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 = Resources().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 = Instance();Locale locale = Resources().getConfiguration().locale;return
本文发布于:2024-02-05 00:13:35,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170719600761102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |