Google公共电话号码解析库: libphonenumber
2017-06-19 11:52
459 查看
libphonenumber是用于解析、格式化、存储和校验电话号码的Java、C++或JavaScript类库。2010年,libphonenumber的Java实现首次以开源形式发布。在2014年,libphonenumber成为Debian发行版的一部分。其中Java版本优化用于运行在智能手机上,并且用在了
Android framework 4.0 (Ice Cream Sandwich)以上的版本中。
GitHub 地址:https://github.com/googlei18n/libphonenumber
Parsing/formatting/validating phone numbers for all countries/regions of the world.
distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).
provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.
by using only the length information, much faster than a full validation.
prefix information.
number.
number.
Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a
At this point, swissNumberProto contains:
For details on the meaning of each field, refer to https://github.com/googlei18n/libphonenumber/blob/master/resources/phonenumber.proto
Now let us validate whether the number is valid:
There are a few formats supported by the formatting method, as illustrated below:
You could also choose to format the number in the way it is dialed from another country:
项目中使用实例:
Android framework 4.0 (Ice Cream Sandwich)以上的版本中。
GitHub 地址:https://github.com/googlei18n/libphonenumber
功能亮点:
Parsing/formatting/validating phone numbers for all countries/regions of the world.getNumberType- gets the type of the number based on the number itself; able to
distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).
isNumberMatch- gets a confidence level on whether two numbers could be the same.
getExampleNumber/
getExampleNumberByType-
provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.
isPossibleNumber- quickly guessing whether a number is a possible phonenumber
by using only the length information, much faster than a full validation.
isValidNumber- full validation of a phone number for a region using length and
prefix information.
AsYouTypeFormatter- formats phone numbers on-the-fly when users enter each digit.
findNumbers- finds numbers in text input.
PhoneNumberOfflineGeocoder- provides geographical information related to a phone
number.
PhoneNumberToCarrierMapper- provides carrier information related to a phone number.
PhoneNumberToTimeZonesMapper- provides timezone information related to a phone
number.
快速开始
Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumberobject:
String swissNumberStr = "044 668 18 00"; PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); try { PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH"); } catch (NumberParseException e) { System.err.println("NumberParseException was thrown: " + e.toString()); }
At this point, swissNumberProto contains:
{ "country_code": 41, "national_number": 446681800 }
PhoneNumberis a class that is auto-generated from the phonenumber.proto with necessary modifications for efficiency.
For details on the meaning of each field, refer to https://github.com/googlei18n/libphonenumber/blob/master/resources/phonenumber.proto
Now let us validate whether the number is valid:
boolean isValid = phoneUtil.isValidNumber(swissNumberProto); // returns true
There are a few formats supported by the formatting method, as illustrated below:
// Produces "+41 44 668 18 00" System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.INTERNATIONAL)); // Produces "044 668 18 00" System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.NATIONAL)); // Produces "+41446681800" System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.E164));
You could also choose to format the number in the way it is dialed from another country:
// Produces "011 41 44 668 1800", the number when it is dialed in the United States. System.out.println(phoneUtil.formatOutOfCountryCallingNumber(swissNumberProto, "US"));
Formatting
Phone Numbers 'as you type'
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US"); System.out.println(formatter.inputDigit('6')); // Outputs "6" ... // Input more digits System.out.println(formatter.inputDigit('3')); // Now outputs "650 253"
项目中使用实例:
/** 代码示例 StringUtil 为String工具类,并未提供,可执行更换。 可以使用String 原生函数替换 */ public class PhoneUtil { private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); 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 = StringUtil.toInteger(countryCode); long phone = StringUtil.toLong(phoneNumber, 0L); PhoneNumber pn = new 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 = StringUtil.toInteger(countryCode); long phone = StringUtil.toLong(phoneNumber, 0L); PhoneNumber pn = new PhoneNumber(); pn.setCountryCode(ccode); pn.setNationalNumber(phone); //返回结果只有英文,自己转成成中文 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; } return carrierZh; } /** * * @Description: 根据国家代码和手机号 手机归属地 * @param @param phoneNumber * @param @param countryCode * @param @return 参数 * @throws */ public static String getGeo(String phoneNumber, String countryCode){ int ccode = StringUtil.toInteger(countryCode); long phone = StringUtil.toLong(phoneNumber, 0L); PhoneNumber pn = new PhoneNumber(); pn.setCountryCode(ccode); pn.setNationalNumber(phone); return geocoder.getDescriptionForNumber(pn, Locale.CHINESE); } }
相关文章推荐
- libphonenumber:Google的公共电话号码解析库
- 用来解析,格式化,存储和验证国际电话号码:libphonenumber
- google 手机号码校验库libphonenumber
- google 手机号码校验库libphonenumber
- 使用谷歌libphonenumber获取号码归属地
- libphonenumber 使用 以及判断国家代码 号码是否有效运营商等
- Android libphonenumber Demo 手机号码归属地
- Google中国2014校园招聘笔试Round A China New Grad Test Problem A. Read Phone Number
- [LeetCode]-Letter Combinations of a Phone Number 电话键盘的字符串组合
- 利用com.googlecode.libphonenumber注册手机号
- 使用PhoneNumberValidator判断用户输入的电话格式,并用PhoneFormatter对电话号码格式化。
- Go实战--go中使用libphonenumber(The way to go)
- 手机号码格式化输入工具 PhoneNumberFormatUtils
- leetcode:Letter Combinations of a Phone Number(手机号码的字母组合)
- 手机号归属地 libphonenumber
- 举例说明在android中用PhoneStateListener监听手机电话状态与用发送邮件来通知来电电话的号码
- Android接电话流程、Phone来电过程、phone上层来电话解析 接通电话
- Introduction to `libphonenumber`
- Google of Greater China Test for New Grads of 2014---很水的一道题Problem A. Read Phone Number