代码之家  ›  专栏  ›  技术社区  ›  Ovi

Android:从联系人中删除国家代码

  •  0
  • Ovi  · 技术社区  · 9 年前

    如何有效删除或检测联系人号码的国家/地区代码?

    +213231756将为231756(+231是阿尔及利亚的国家代码)

    +880171876809将是01718768(+88是BD的国家代码)

    例如:我创建了一个字符串。类似xml文件

    <string-array name="CountryCodes" >
    <item>93,AF</item> 
    <item>355,AL</item>
    <item>213,DZ</item>
    

    xml中有将近190多个国家代码。 并且用字符串数组检查每个联系人号码的时间效率不高。

    是否有任何有效的解决方案来检测电话号码列表的国家代码。

    1 回复  |  直到 5 年前
        1
  •  0
  •   Sunil Sunny    9 年前

    您可以使用“libphonenumber.jar”进行此操作。 http://repo1.maven.org/maven2/com/googlecode/libphonenumber/libphonenumber/7.3.1/

    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    try {
        // phone must begin with '+'
        PhoneNumber numberProto = phoneUtil.parse(phone, "");
        int countryCode = numberProto.getCountryCode();
    } catch (NumberParseException e) {
        System.err.println("NumberParseException was thrown: " + e.toString());
    }