How can I get the country code for CDMA Android devices?
Asked Answered
B

3

3

How can I retrieve the country code information for Android devices under CDMA networks?

For all others, you can just use the TelephonyManager for that:

String countryCode = null;
TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (telMgr.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA)
    countryCode = telMgr.getNetworkCountryIso();
}
else {
    // Now what???
}

I searched a bit, but I did not find any useful information that would lead to an answer.

Some ideas some to mind:

  • GPS location: you can get the country from GeoCoder; and
  • IP address: there are some nice APIs to get it, such as ipinfodb.

Could these be used or are there better ones?

Bram answered 28/11, 2011 at 6:37 Comment(0)
I
1

It works for CDMA, but not always - it 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...

Irvine answered 1/12, 2011 at 14:56 Comment(1)
Hei, that's what I am doing to get the phone number. I guess it is just easier to rely on this then. Thanks a lot!Bram
G
2

I found a way to tackle this problem... If it is a CDMA phone, then the phone always has ICC hardware comparable to SIM cards in GSM.

All you have got to do is use the system properties associated with the hardware. Programmatically, you can use Java reflection to get this information. This is not changeable, even if the system is rooted, unlike a 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
Growl answered 23/7, 2012 at 19:2 Comment(1)
This answer is incomplete. MCC and MNC are numeric. Question asks for country codes in comparison to getNetworkCountryIso(); so alphabet-based ISO country codes.Refraction
I
1

It works for CDMA, but not always - it 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...

Irvine answered 1/12, 2011 at 14:56 Comment(1)
Hei, that's what I am doing to get the phone number. I guess it is just easier to rely on this then. Thanks a lot!Bram
U
1

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 Wikipedia 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 "";
}
Unconscionable answered 2/9, 2018 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.