意外的telephonyManager.getSimCountryIso()行为 [英] Unexpected telephonyManager.getSimCountryIso() behaviour

查看:655
本文介绍了意外的telephonyManager.getSimCountryIso()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,该应用程序将根据您的移动网络提供商所在的国家/地区显示该国家/地区的所有替代移动网络提供商的列表.为此,我正在使用 telephonyManager.getSimCountryIso( ).

I am creating an app that depending on which country your mobile network provider is from, displays a list of all alternative mobile network providers from that same country. To achieve this, I was retrieving the country code using telephonyManager.getSimCountryIso().

官方android开发人员说:返回与SIM卡提供商的国家/地区代码对应的ISO国家/地区代码",因此我希望国家/地区代码始终独立于设备位置而始终相同.但这不是它实际的工作方式! 例如,我最近遇到了这种情况: 我有一个来自西班牙的SIM卡的android设备,该设备属于西班牙网络提供商.因此,如果我在西班牙,TelephonyManager.getSimCountryIso()将返回"es".到那时一切都很好. 问题是例如,当我去法国旅行时,我调试了该应用程序并发现telephonyManager.getSimCountryIso()返回的国家代码为:"nl"(来自荷兰!?并且我在法国漫游但使用相同的西班牙SIM卡!).我在西班牙使用的设备和SIM卡与西班牙相同,因此国家/地区代码ISO仍应为"es".

Official android developer docs say : "Returns the ISO country code equivalent for the SIM provider's country code", so from this I was expecting country code will always be always the same independently from device location. But thats not how it actually works! For example I recently experienced this case: I have an android device with SIM card from Spain belonging to a spanish network provider. So if I am in Spain telephonyManager.getSimCountryIso() returns "es". Everything working fine to that point. The problem is when I travel to France for example I debug the app and find out the telephonyManager.getSimCountryIso() is returning country code: "nl" (from Netherlands!? and I am in France in roaming but with the same spanish SIM card!). I am using the same device and the same SIM card than in Spain so country code ISO should still be "es".

我的问题是这种方法实际上是如何工作的?如果我使用西班牙SIM卡,为什么会收到国家代码"nl"(荷兰)?

My question is How does this method actually work? why am I getting country code "nl" ( Netherlands) if I am using a spanish SIM card?

预先感谢您的帮助

推荐答案

您可以使用 MCC MNC来获取SIM卡所在的国家/地区,它已配置为SIM卡,与您所处的网络无关.

You can use MCC MNC to get SIM country, it is SIM configured and has nothing to do with the network you are on.

Configuration config = getResources().getConfiguration();
int countryCode = config.mcc;

您可以在例如,西班牙为214,法国为208

For example Spain is 214 and France is 208

MCC应该可以在所有带有SIM卡的GSM设备上使用,但是在CDMA网络上并不可靠

MCC should work on all GSM devices with SIM card but it is unreliable on CDMA networks

对于CDMA设备,我找到了以下解决方案

For CDMA devices I found the following solution

if (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
    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 
} else {
    Configuration config = getResources().getConfiguration();
    int countryCode = config.mcc;
}

这篇关于意外的telephonyManager.getSimCountryIso()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆