Android 获取用户所在国家的可靠方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11872483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reliable method to get the country the user is in?
提问by Ixx
I usually get the country from the device's language. It works but now I have to recognize Brazil. And most of the devices only have portuguese (pt_PT), and no portuguese (Brazil) option.
我通常从设备的语言中获取国家/地区。它有效,但现在我必须承认巴西。而且大多数设备只有葡萄牙语(pt_PT),没有葡萄牙语(巴西)选项。
I checked this thread: Where am I? - Get country
我检查了这个线程:我在哪里?- 获取国家
The methods
方法
String locale = context.getResources().getConfiguration().locale.getCountry();
String locale = context.getResources().getConfiguration().locale.getDisplayCountry();
Are still language-only, doesn't help.
仍然仅限语言,没有帮助。
There's also the suggestion with the sim-card, but I'm not sure if this will work reliably (do all sim cards have this unique identification?), it's also a bit not exactly what I need because the user can't change it (which is the case if it was a setting), and it will exclude users using a device without sim-card (maybe they just use WLAN).
sim 卡也有建议,但我不确定这是否能可靠地工作(所有 sim 卡都有这个唯一标识吗?),它也有点不完全是我需要的,因为用户无法更改它(如果是设置就是这种情况),它会排除使用没有 SIM 卡的设备的用户(也许他们只是使用 WLAN)。
There's also geolocation suggestion, but this will probably not work in devices which have deactivated it. Or am I wrong?
还有地理定位建议,但这可能不适用于已停用它的设备。还是我错了?
If nothing else helps I would make a dialog or menu setting in my app so the user can select it there. But I would first like to confirm if there's any reliable possibility with the device.
如果没有其他帮助,我会在我的应用程序中创建一个对话框或菜单设置,以便用户可以在那里选择它。但我首先想确认该设备是否有任何可靠的可能性。
回答by Eric
You can pull the location from the phone network:
您可以从电话网络中提取位置:
TelephonyManager.getNetworkCountryIso()
或从 SIM 卡:
TelephonyManager.getSimCountryIso()
Or, if you have the user's phone number, you may be able to match it to the country through this data.
或者,如果您有用户的电话号码,您也许可以通过此数据将其与国家/地区进行匹配。
Ideally, you could use all three of these (in some order, perhaps SIM, Phone #, then Network), and if none works, use reverse geolocation as a last resort.
理想情况下,您可以使用所有这三个(按某种顺序,可能是 SIM、电话号码,然后是网络),如果都不起作用,请使用反向地理定位作为最后的手段。
回答by t0mm13b
Try this:
尝试这个:
TelephonyManager teleMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if (teleMgr != null){
countryISOCode = teleMgr.getSimCountryIso();
}
Now, countryISOCode
would contain one of the following similar to the ISO 3166 country codes as per this Wikipedia entry.
现在,countryISOCode
将包含以下类似于此 Wikipedia条目的 ISO 3166 国家/地区代码之一。
回答by Shavi
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.getNetworkCountryIso();
As answered by Eric, the above code is the best approach. It is also worth noting that,
正如 Eric 所回答的,上面的代码是最好的方法。还值得注意的是,
mTelephonyManager.getSimCountryIso()
should not be used as this would indicate the home country of the SIM provider (E.g. A Vodaphone UK SIM would return "gb", A Vodaphone Germany SIM would return "de") and not the current location (Country) of the device. This difference is significant when the user is roaming.
不应使用,因为这将指示 SIM 提供商的母国(例如 Vodaphone UK SIM 将返回“gb”,Vodaphone Germany SIM 将返回“de”)而不是设备的当前位置(国家/地区)。当用户漫游时,这种差异是显着的。
回答by D.A
Geolocation would be the most effective and unobtrusive. You could always use geolocation, and if the user has it disabled display your dialog asking for their country. The less you bug the user for information the better.
地理定位将是最有效和最不引人注目的。您始终可以使用地理定位,如果用户禁用了它,则显示您的对话框,询问他们的国家/地区。你越少让用户获取信息越好。
回答by abir99
It's working on me.
它在我身上起作用。
String countryISOCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager teleMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if (teleMgr != null){
countryISOCode = teleMgr.getSimCountryIso();
}
............................
…………………………………………………………………………………………………………………………………………………………………………