How to get country code for Android CDMA devices?

Does anyone know how to get country code information for Android devices on CDMA networks?

For everyone else, you can simply use TelephonyManager to do this:

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 little, but did not find useful information that could lead to an answer. Some ideas to think about:

  • GPS location: you can get the country from GeoCoder ; and
  • IP Address: There are several useful APIs to get it, such as ipinfodb .

Has anyone done one of the above approaches or implemented the best?

Thanks for the help.

+4
source share
3 answers

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

Here is an alternative idea which suggests looking at outgoing SMS or calls to find out the phone number of this device, from which you can out CountryIso based on the international dialing code ...

Hope this helps

+1
source

I found a way to solve this problem. If it is a CDMA phone, then the phone is always equipped with ICC equipment comparable to SIM cards in GSM. All you have to do is use the system properties related to the hardware. Programmatically, you can use Java reflection to obtain this information. This is not changeable, even the system is implemented as opposed to 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 
+2
source

based on @rana's answer, here is the full code, including security and mapping to ISO country code

I only compare 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 ""; } 
+1
source

Source: https://habr.com/ru/post/1383380/


All Articles