使用电话号码获取来电国家/地区名称?

时间:2011-07-21 08:28:02

标签: android

您好我们可以在Android手机上获取来电的国家名称吗?

2 个答案:

答案 0 :(得分:2)

  1. Get the calling number.
  2. 在其中查找国家/地区代码
  3. 在地图中查找国家/地区。您可以使用here来构建它。
  4. 希望它有所帮助。

答案 1 :(得分:0)

使用libphonenumber

public String convertPhoneNumber(String phoneNumber, PhoneNumberUtil.PhoneNumberFormat format) {
    String resultNumber = "";
    Phonenumber.PhoneNumber myNumberProto = null;
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    if (!phoneNumber.substring(0, 1).equals("+")) {
        try {
            myNumberProto = phoneUtil.parse(phoneNumber,
                    Locale.getDefault().getCountry().toUpperCase());
            resultNumber = phoneUtil.format(myNumberProto,
                    format);
        } catch (NumberParseException e) {
            System.err.println("NumberParseException was thrown: " + e.toString());
        }
    } else {
        try {
            myNumberProto = phoneUtil.parse(phoneNumber, "ZZ");
            resultNumber = phoneUtil.format(myNumberProto,
                    format);
        } catch (NumberParseException e) {
            System.err.println("NumberParseException was thrown: " + e.toString());
        }
    }

    return resultNumber;
}


public String getRegionOfPhoneNumber(String callNumber) {
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    Phonenumber.PhoneNumber myNumberProto = null;
    callNumber = convertPhoneNumber(callNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
    try {
        myNumberProto = phoneUtil.parse(callNumber, "ZZ");
    } catch (NumberParseException e) {
        e.printStackTrace();
    }
    String result = "";
    if (myNumberProto != null) {
        result = phoneUtil.getRegionCodeForCountryCode(myNumberProto.getCountryCode());
    }
    return result;
}

public String getCountryNameOfPhoneNumber(String callNumber) {
    String result = "";
    String regionCode = getRegionOfPhoneNumber(callNumber);
    if (!regionCode.equals("")) {
        result = new Locale("", regionCode).getDisplayCountry(Locale.getDefault());
    }
    return result;
}

用法:     getCountryNameOfPhoneNumber("电话号码");

希望这有助于某人