2016-03-04 89 views
0

我想使用SIM卡获取设备国家代码。Android获取设备国家代码

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

我的最终目标就是让调用代码(+94斯里兰卡)

我试着用这个

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

但这返回空字符串

+0

使用这些它可能有助于您: - TelephonyManager.getNetworkCountryIso() –

+0

可点击以下链接帮助你出去的http:// stackoverflow.com/questions/5402253/getting-telephone-country-code-with-android –

+0

看看这个:http://stackoverflow.com/a/11872569/5352802 – camelCaseCoder

回答

1

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

您也许能够use the device's current locale

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

但是这不一定会是一样的,你要通过看载体iso得到什么,并且用户可以更新,在设置时,他们想要什么他们想要的。正如指出的那样,您还可以向位置管理器询问用户的坐标。但请记住,用户四处移动,他们的当前位置可能并不总是与其运营商的iso相对应。

+0

谢谢炒。我也试过这个,但它总是返回“美国”。 –

+1

@ayeshdon这是因为本地设置为美国。我再说一遍,如果没有SIM卡,你***无法获得载体'iso' *。 – dcow

+0

这将返回用户配置的区域设置的国家。如果用户将设备配置为印度,然后将其搭载到德国的飞机上,您仍然可以获得印度的国家代码。只是想我会澄清这一点。 –

0

您可以使用此http://maps.googleapis.com/maps/api/geocode/json?latlng=7.0000,81.0000&sensor=false用于获取国家代码..The简称会给你的国家代码..

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和长..

使用此链接获取当前LAT和液化天然气

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

+0

如果我不在我的航空公司国家怎么办? – dcow

+0

他问不使用SIM卡的设备国家代码,这是我猜测的唯一选项。如果用户的运营商国家和当前的国家不同,那么这是行不通的。 –