美文网首页Android
Google公共电话号码解析库: libphonenumber

Google公共电话号码解析库: libphonenumber

作者: OnlyRose | 来源:发表于2018-09-13 20:06 被阅读488次

    libphonenumber是用于解析、格式化、存储和校验电话号码的Java、C++或JavaScript类库。2010年,libphonenumber的Java实现首次以开源形式发布。在2014年,libphonenumber成为Debian发行版的一部分。其中Java版本优化用于运行在智能手机上,并且用在了 Android framework 4.0 (Ice Cream Sandwich)以上的版本中。

    GitHub 地址:<u>https://github.com/googlei18n/libphonenumber</u>

    基本概念

    一些基本概念的介绍

    国家代码

    国家代码(或国家编码)是一组用来代表国家和境外领土的地理代码。国家代码是由字母或数字组成的短字串,方便用于数据处理和通讯。世界上有许多不同的国家代码标准,其中最广为人知是为国际标准化组织的<u>ISO 3166-1</u>。国家代码也可以指<u>国际长途电话</u>国家号码,即<u>国际电信联盟</u>的国际电话区号(E.164)

    中国86

    港澳台地区 香港852澳门853台湾886

    国际冠码

    <u>国际电信联盟</u>早些前已认可00为通用的国际冠码,并被多数国家所采用,但仍有一些国家决定采用不同的国际冠码,甚至提供多组冠码。

    image.png

    国际电话

    拨打国际电话的一般顺序是:国际冠码-国际电话区号-国内电话区号-开放电话号码;国际冠码-国际电话区号-封闭电话号码。

    国际电话区号

    国际电话区号,即<u>国际电信联盟</u>根据E.164标准分配给各国的代码。所有的号码都是前缀号,也就是说这些号码是用来“拨到”目的国家。每一个国家还有一个前缀来“拨出”所在国家,这个前缀叫<u>国际冠码</u>

    image.png

    E164号码

    E.164 是国际电信联盟定义的在PSTN和一些数据网使用的国际公共电话码号方案,同时定义了具体的码号的格式。E.164定义了最大15数字,完整号码有国际呼叫前缀。
    E.164号码是MSISDN号码,它是主叫用户为呼叫移动通信网中用户所需拨号的号码。
    其格式为:CC+NDC+SN,也可以表示为:国家代码+N1N2N3+H0H1H2H3+ABCD
    (CC=国家码,中国为86;NDC=国内目的码;SN=用户号码)
    例如给中国广东深圳0755-12345678拨号,处理后的结果是+8675512345678。其中+号表示要进行国际拨号,在拨号到运营商网络时候会自动(gsm会,cdma不一定)转成一个号码(中国就是00,+86在中国打的话其实就是0086),这个号码代表拨号时注册的运营商网络所在国家;86代表目的所在国家代码,中国的是86;755代表国内地区码(中国很大,按地区分,有些小国根本不需要),0755中的0是国内长途接入码,例如国内座机拨打外地手机号要加0,国际拨号的时候不需要;最后是目的地的号码,座机或者手机号码。
    https://blog.csdn.net/qq_33645265/article/details/53609609
    1.快速集成
    jar包下载地址
    http://repo1.maven.org/maven2/com/googlecode/libphonenumber/
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/carrier-1.9.jar')
    compile files('libs/geocoder-2.93.jar')
    compile files('libs/libphonenumber-8.9.4.jar')
    compile files('libs/prefixmapper-2.93.jar')

    /**
         * 根据国家代码和手机号  判断手机运营商
         *
         * @param phoneNumber
         * @return
         */
        public static String getCarrier(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);
            //返回结果只有英文,自己转成成中文
            String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
            String carrierZh = "";
            if (countryCode == 86 && Locale.CHINA.getCountry().equals(Locale.getDefault().getCountry())) {
                switch (carrierEn) {
                    case "China Mobile":
                        carrierZh += "中国移动";
                        break;
                    case "China Unicom":
                        carrierZh += "中国联通";
                        break;
                    case "China Telecom":
                        carrierZh += "中国电信";
                        break;
                    default:
                        break;
                }
                return carrierZh;
            } else {
                return carrierEn;
            }
        }
    
        /**
         * 根据国家代码和手机号判断归属地
         *
         * @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);
    
        }
    
    
        /**
         * 根据国家代码和手机号  判断手机号是否有效
         *
         * @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 mPhoneNumberUtil.isValidNumber(pn);
    
        }
    

    相关文章

      网友评论

        本文标题:Google公共电话号码解析库: libphonenumber

        本文链接:https://www.haomeiwen.com/subject/zneggftx.html