|
Project Information
Links
|
What is it?Google's common Java, C++ and Javascript library for parsing, formatting, storing and validating international phone numbers. The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich). Highlights of functionality
DemoJava Version (running code r715) JavaScript Version (running JS code r715) CodeTo include the code in your application, either integrate with Maven or download the latest Jars from the Maven repository: http://repo1.maven.org/maven2/com/googlecode/libphonenumber/libphonenumber/ Quick ExamplesLet's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumber object: 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
}PhoneNumber is 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://code.google.com/p/libphonenumber/source/browse/trunk/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"Geocoding Phone Numbers offlinePhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance(); // Outputs "Zurich" System.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.ENGLISH)); // Outputs "Zürich" System.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.GERMAN)); // Outputs "Zurigo" System.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.ITALIAN)); Mapping Phone Numbers to carrierPhoneNumber swissMobileNumber =
new PhoneNumber().setCountryCode(41).setNationalNumber(798765432L);
PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
// Outputs "Swisscom"
System.out.println(carrierMapper.getNameForNumber(swissMobileNumber, Locale.ENGLISH));More examples on how to use the library can be found in the unittests at http://code.google.com/p/libphonenumber/source/browse/#svn/trunk/java/libphonenumber/test/com/google/i18n/phonenumbers Known PortsSeveral people are porting the phone number library to other languages. Here are some we know about. Note that they are done on voluntary basis by developers outside our project, so we cannot guarantee their quality. |