I am allowing the user to choose a phone number from their address book. I need the number to always be in the international format, however sometimes people store a local number without the country code in their contacts (ex. 555-555-5555 instead of +1-555-555-5555).

Is there an easy way to find out what country code the local number implies so I can add it manually?

有帮助吗?

解决方案

This is what you need https://code.google.com/p/libphonenumber/

String numberString = "044 668 18 00"

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  PhoneNumber numberProto = phoneUtil.parse(numberString, "BH"); // Where BH is the user's iso country code
  String finalNumber = phoneUtil.format(numberProto, PhoneNumberFormat.E164);
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top