1

我允许用户从他们的通讯录中选择一个电话号码。我需要该号码始终采用国际格式,但有时人们会在联系人中存储没有国家代码的本地号码(例如 555-555-5555 而不是 +1-555-555-5555)。

有没有一种简单的方法可以找出本地号码所暗示的国家代码,以便我可以手动添加?

4

1 回答 1

2

这就是你需要的 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());
}
于 2013-08-26T19:25:09.073 回答