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

问题描述 投票:4回答: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

有没有人做过上述方法之一,或实施更好的方法?

谢谢您的帮助。

android telephony telephonymanager cdma
3个回答
1
投票

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

这是一个alternative idea,它建议查看外出短信或呼叫以找出该设备的电话号码,然后您可以根据国际拨号代码找出CountryIso ...

希望这可以帮助


2
投票

我找到了解决这个问题的方法..如果它是CDMA手机,那么手机的ICC硬件总是与GSM中的SIM卡相当。您所要做的就是使用与硬件相关的系统属性。以编程方式,您可以使用Java反射来获取此信息。即使系统与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
投票

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

我基于this wiki page映射了实际使用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 "";
}
© www.soinside.com 2019 - 2024. All rights reserved.