How to get the country code for CDMA Android devices?

情到浓时终转凉″ 提交于 2019-11-28 01:22:44

It works for CDMA, but not always - depends on the network carrier.

Here's an alternative idea, which suggests looking at the Outgoing SMS or Calls to figure out this device's phone number, from which you can then figure out the CountryIso based on the international dialing code...

Hope this helps

I found a way to tackle this problem .. if it is a CDMA phone , then the phone always has an ICC hardware comparable to SIM cards in GSM. All you gotta do is use the system properties associated with the hard ware . Programmatically you can use Java reflection to get this information . This is not changeable even system is rooted unlike GSM device.

        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 

based on @rana's reply, here's the full code, including safety and mapping to the ISO country code

I'm mapping just countries that actually use CDMA networks, based on this wiki page.

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 "";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!