在启动应用程序时获取Telephonic国家/地区代码

时间:2017-01-29 11:24:00

标签: android

我正在尝试通过在应用启动时阅读地理位置来获取国家/地区 telephonic 代码。

但我只能获得纬度经度。如何从地理位置获取电话代码。

我做到了这一点远看:

首先在String xml中我会像这样创建一个数组

<string-array name="CountryCodes" >
<item>91,IN</item>
.....
.....
.....
</string-array>

public String GetCountryZipCode(){
    String CountryID="";
    String CountryZipCode="";

    TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    //getNetworkCountryIso
    CountryID= manager.getSimCountryIso().toUpperCase();
    String[] rl=this.getResources().getStringArray(R.array.CountryCodes);
    for(int i=0;i<rl.length;i++){
        String[] g=rl[i].split(",");
        if(g[1].trim().equals(CountryID.trim())){
            CountryZipCode=g[0];
            break;  
        }
    }
    return CountryZipCode;
}

现在假设我读了地理定位,但我怎么知道国家名称或国家代码(如果我知道的话更容易)

1 个答案:

答案 0 :(得分:0)

根据文件getSimCountryIso

  

返回与SIM卡提供商的国家/地区代码等效的ISO国家/地区代码。

有一些(奇怪的)行为可以查看Unexpected getSimCountryIso() behaviour,以获得最佳体验Mobile country code (MCC)。您可以使用以下代码在Android中找到MCC

TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getNetworkOperator();
if (!TextUtils.isEmpty(networkOperator)) {
    int mcc = Integer.parseInt(networkOperator.substring(0, 3));
    int mnc = Integer.parseInt(networkOperator.substring(3));
}

您可以找到MCC列表herehere。在您的应用程序中保存MCC列表(来自上述任何链接),并通过MCC或MCCMNC查找国家/地区代码。