如何获取CDMA Android设备的国家代码?

3
如何在CDMA网络下检索Android设备的国家代码信息?
对于其他所有情况,您只需使用 TelephonyManager即可:
String countryCode = null;
TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (telMgr.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA)
    countryCode = telMgr.getNetworkCountryIso();
}
else {
    // Now what???
}

我进行了一些搜索,但没有找到任何有用的信息来回答问题。

一些想法浮现:

  • GPS位置:您可以从GeoCoder获取国家;和
  • IP地址:有一些很好的API可以获取它,例如ipinfodb

这些可以使用吗?还是有更好的选择?

3个回答

2
我找到了解决这个问题的方法...如果是CDMA手机,那么手机始终具有与GSM中的SIM卡相当的ICC硬件。
你需要做的就是使用与硬件相关联的系统属性。在编程时,您可以使用Java反射来获取此信息。即使系统已经root,这也是不可更改的,不像GSM设备。
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);

// Gives MCC + MNC
String homeOperator = ((String) get.invoke(c, "ro.cdma.home.operator.numeric"));
String country = homeOperator.substring(0, 3); // The last three digits is MNC

1
这个答案不完整。MCC和MNC是数字代码。问题要求与getNetworkCountryIso();相比较的国家代码,因此是基于字母表的ISO国家代码。 - androidguy

1

它适用于CDMA,但并非总是如此 - 这取决于网络运营商。

这里有另一种想法,建议查看发送的SMS或通话以确定该设备的电话号码,然后可以根据国际拨号代码确定CountryIso...


嘿,这就是我获取电话号码的方法。我猜这比其他方式更容易依赖。非常感谢! - bruno.braga

1

根据rana的回复,这里是完整的代码,包括安全性和映射到ISO国家代码。

我仅映射实际使用CDMA网络的国家,基于这个维基百科页面

private static String getCdmaCountryIso() {
    try {
        @SuppressLint("PrivateApi")
        Class<?> c = Class.forName("android.os.SystemProperties");
        Method get = c.getMethod("get", String.class);
        String homeOperator = ((String) get.invoke(c, "ro.cdma.home.operator.numeric")); // MCC + MNC
        int mcc = Integer.parseInt(homeOperator.substring(0, 3)); // just MCC

        switch (mcc) {
            case 330: return "PR";
            case 310: return "US";
            case 311: return "US";
            case 312: return "US";
            case 316: return "US";
            case 283: return "AM";
            case 460: return "CN";
            case 455: return "MO";
            case 414: return "MM";
            case 619: return "SL";
            case 450: return "KR";
            case 634: return "SD";
            case 434: return "UZ";
            case 232: return "AT";
            case 204: return "NL";
            case 262: return "DE";
            case 247: return "LV";
            case 255: return "UA";
        }
    }
    catch (ClassNotFoundException ignored) {
    }
    catch (NoSuchMethodException ignored) {
    }
    catch (IllegalAccessException ignored) {
    }
    catch (InvocationTargetException ignored) {
    }
    catch (NullPointerException ignored) {
    }
    return "";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接