問題描述
Android 手機實際上確實很清楚它在哪里 - 但是有沒有辦法通過國家代碼之類的東西來檢索國家?
An android mobile actually does know quite well where it is - but is there a way of retrieving the country by something like a country code?
無需知道準確的 GPS 位置 - 國家 就足夠了
No need of knowing the exact GPS position - the country is sufficient
我最初考慮使用時區,但實際上我需要更多信息,因為如果位置是紐約或利馬,它會有所不同.
I first thought of using the time zone, but actually I need more information than that since it makes a difference if the location is New York or Lima.
問題的背景: 我有一個使用溫度值的應用程序,我想將默認單位設置為攝氏度或華氏度,具體取決于位置是美國還是國外
The background of the question: I have an application that uses temperature values, and I'd like to set the default unit either to Celsius or Fahrenheit, depending on whether the location is US or outside
推薦答案
這將得到 國家代碼 為手機設置(手機語言,而不是用戶位置):
This will get the country code set for the phone (phones language, NOT user location):
String locale = context.getResources().getConfiguration().locale.getCountry();
也可以用 getISO3Country() 獲取國家/地區的 3 個字母 ISO 代碼.這將獲得 國家/地區名稱:
can also replace getCountry() with getISO3Country() to get a 3 letter ISO code for the country. This will get the country name:
String locale = context.getResources().getConfiguration().locale.getDisplayCountry();
這似乎比其他方法更容易,并且依賴于手機上的本地化設置,所以如果美國用戶在國外,他們可能仍然想要華氏度,這會起作用:)
This seems easier than the other methods and rely upon the localisation settings on the phone, so if a US user is abroad they probably still want Fahrenheit and this will work :)
編者注:此解決方案與手機的位置無關.它是恒定的.當您前往德國時,語言環境不會改變.簡而言之:locale != location.
Editors note: This solution has nothing to do with the location of the phone. It is constant. When you travel to Germany locale will NOT change. In short: locale != location.
這篇關于我在哪里?- 獲取國家的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!