Android使用libphonenumber库查询归属地

Android大约 7717 字

可以三个包重新打包成一个包

 jar vcf xxx.jar *

添加依赖

compile 'com.googlecode.libphonenumber:libphonenumber:8.5.1'
compile 'com.googlecode.libphonenumber:carrier:1.62'
compile 'com.googlecode.libphonenumber:geocoder:2.72'

工具类

public class GeoUtil {

    private static final String TAG                         = "zbj0527";

    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 {
            return mPhoneNumberUtil.parse(phoneNumber,getCurrentCountryIso(context));
        } catch (NumberParseException e) {
            Log.d(TAG, "getStructedNumber:NumberParseException: " + e);
            return  null;
        }
    }

    //获取运营商信息
    public static String getCarrier(Context context,String phoneNumber) {

        Phonenumber.PhoneNumber structedNumber = getStructedNumber(context,phoneNumber);
        return carrierMapper.getNameForNumber(structedNumber,Locale.US);
    }
}
public class CountryDetector {
    private static final String TAG = "zbj0527";
    private static final String DEFAULT_COUNTRY_ISO = "CN";
    private static CountryDetector  sInstance;
    private final  TelephonyManager mTelephonyManager;

    private CountryDetector(Context context) {
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }

    public static CountryDetector getInstance(Context context) {
        if(sInstance == null) {
            synchronized (CountryDetector.class) {
                if (sInstance == null) {
                    sInstance = new CountryDetector(context);
                }
            }
        }
        return sInstance;
    }

    public String getCurrentCountryIso() {
        String result = null;
        boolean networkCountryCodeAvailable = isNetworkCountryCodeAvailable();
        Log.d(TAG, "networkCountryCodeAvailable: " + networkCountryCodeAvailable);
        if (networkCountryCodeAvailable) {
            result = getNetworkBasedCountryIso();
            Log.d(TAG," getNetworkBasedCountryIso: " + result);
        }
        if (TextUtils.isEmpty(result)) {
            result = getSimBasedCountryIso();
            Log.d(TAG,"getSimBasedCountryIso: " + result);
        }
        if (TextUtils.isEmpty(result)) {
            result = getLocaleBasedCountryIso();
            Log.d(TAG,"getLocaleBasedCountryIso: " + result);
        }
        if (TextUtils.isEmpty(result)) {
            result = DEFAULT_COUNTRY_ISO;
            Log.d(TAG,"DEFAULT_COUNTRY_ISO");
        }
        Log.d(TAG," result ==  " + result + "\n-------------------------------------------------");
        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 = Locale.getDefault();
        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;
    }
}

或者

public class PhoneUtil {

    private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

    private static int countryCode = phoneNumberUtil.getCountryCodeForRegion(Locale.getDefault().getCountry());

    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();

    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

    /**
     * 根据国家代码和手机号  判断手机号是否有效
     * @param phoneNumber
     * @param countryCode
     * @return
     */
    public static boolean checkPhoneNumber(String phoneNumber, String countryCode){
        int ccode = Integer.valueOf(countryCode);
        long phone = Long.valueOf(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);

        return phoneNumberUtil.isValidNumber(pn);

    }

    /**
     * 根据国家代码和手机号  判断手机运营商
     * @param phoneNumber
     * @param countryCode
     * @return
     */
    public static String getCarrier(String phoneNumber, String countryCode){

        int ccode = Integer.valueOf(countryCode);
        long phone = Long.valueOf(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);

        PhoneNumberUtil.PhoneNumberType numberType = phoneNumberUtil.getNumberType(pn);

        //返回结果只有英文,自己转成成中文
        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        carrierZh += geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
        switch (carrierEn) {
        case "China Mobile":
            carrierZh += "移动";
            break;
        case "China Unicom":
            carrierZh += "联通";
            break;
        case "China Telecom":
            carrierZh += "电信";
            break;
        default:
            break;
        }
        carrierZh += numberType.name();
        return carrierZh;
    }


    /**
     * 
    * @Description: 根据国家代码和手机号  手机归属地
    * @date 2015-7-13 上午11:33:18
    * @param @param phoneNumber
    * @param @param countryCode
    * @param @return    参数
    * @throws
     */
    public static String getGeo(String phoneNumber){

        Log.wtf("zbj0525", "countryCode: "+countryCode );
        long phone = Long.valueOf(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(countryCode);
        pn.setNationalNumber(phone);

        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);

    }

    public static void main(String[] args) {
            System.out.println(PhoneUtil.getCarrier("159655555","86"));

    }

}
阅读 1201 · 发布于 2019-04-14

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批