Android获取设备国家代码

时间:2016-03-04 06:46:52

标签: android

我想使用SIM卡获取设备国家/地区代码。

例如,如果国家:斯里兰卡        国家代码:LK

我的最终目标是调用代码(sri lanka为+94)

我试过这个

TelephonyManager tm = (TelephonyManager)this.getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();

但这会返回空字符串

4 个答案:

答案 0 :(得分:2)

它返回一个空字符串,因为该设备没有SIM卡。

您可以use the device's current locale

Locale current = getResources().getConfiguration().locale;

但这不一定与通过查看运营商iso获得的内容相同,用户可以随时根据需要更新设置。如前所述,您还可以向位置管理器询问用户的坐标。但请记住,用户四处移动,他们当前的位置可能并不总是与其运营商的iso对应。

答案 1 :(得分:0)

您可以使用此http://maps.googleapis.com/maps/api/geocode/json?latlng=7.0000,81.0000&sensor=false获取国家/地区代码..短名称将为您提供国家/地区代码..

 public void getAddress(double lat,double lng) {
    Address1 = "";
    Address2 = "";
    City = "";
    State = "";
    Country = "";
    County = "";
    PIN = "";

    try {

        JSONObject jsonObj = JsonParser.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + ","
                +lng + "&sensor=false");
        String Status = jsonObj.getString("status");
        if (Status.equalsIgnoreCase("OK")) {
            JSONArray Results = jsonObj.getJSONArray("results");
            JSONObject zero = Results.getJSONObject(0);
            JSONArray address_components = zero.getJSONArray("address_components");

            for (int i = 0; i < address_components.length(); i++) {
                JSONObject zero2 = address_components.getJSONObject(i);
                String long_name = zero2.getString("long_name");
                String short_name = zero2.getString("short_name");
                JSONArray mtypes = zero2.getJSONArray("types");
                String Type = mtypes.getString(0);

                if (TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "") {
                    if (Type.equalsIgnoreCase("street_number")) {
                        Address1 = long_name + " ";
                    } else if (Type.equalsIgnoreCase("route")) {
                        Address1 = Address1 + long_name;
                    } else if (Type.equalsIgnoreCase("sublocality")) {
                        Address2 = long_name;
                    } else if (Type.equalsIgnoreCase("locality")) {
                        // Address2 = Address2 + long_name + ", ";
                        City = long_name;
                    } else if (Type.equalsIgnoreCase("administrative_area_level_2")) {
                        County = long_name;
                    } else if (Type.equalsIgnoreCase("administrative_area_level_1")) {
                        State = long_name;
                    } else if (Type.equalsIgnoreCase("country")) {
                        Country = short_name;
                    } else if (Type.equalsIgnoreCase("postal_code")) {
                        PIN = long_name;
                    }
                }

                // JSONArray mtypes = zero2.getJSONArray("types");
                // String Type = mtypes.getString(0);
                // Log.e(Type,long_name);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

但是你需要获得当前的lat和long ..

使用此链接获取当前的lat和lng

http://gabesechansoftware.com/location-tracking/

答案 2 :(得分:0)

In my case telephonyManager.getNetworkCountryIso()  is returning empty as i have two sim slots, you can pass slot as parameter if it returns ""

simply change your code to:
 String iso=telephonyManager.getNetworkCountryIso();
       if(TextUtils.isEmpty(iso))
           iso=telephonyManager.getNetworkCountryIso(1);
       if(TextUtils.isEmpty(iso))
           iso=telephonyManager.getNetworkCountryIso(2);

this code will give preference to Sim Slot 1

答案 3 :(得分:-4)

  1. 获取iso国家代码=&gt; US
  2. 查找此表=&gt; 1 https://countrycode.org/