2016-03-04 84 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卡的設備國家代碼,這是我猜測的唯一選項。如果用戶的運營商國家和當前的國家不同,那麼這是行不通的。 –