获取基于用户当前位置的Android国家code [英] Get country code based on user's current location Android
问题描述
我已经尝试了以下code获得国家code名称
I have already tried out the following code to get country code name
TelephonyManager tm = (TelephonyManager)getActivity().getSystemService(getActivity().TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
但我想,如果我想获得当前的国家code中,用户是这不起作用。
But I guess this does not work if I want to get current country code in which the user is.
例如,如果用户旅行从一个国家到另一个国家我想获得该国code,他是在
For example if user travels from one country to another I want to get the country code in which he is in.
我有一个解决方案,使用户的当前国家code,但我坚持:
I have a solution to get user's current country code but I am stuck:
- 我获取用户当前位置。
- 使用此当前位置,我可以得到国家名获取用户的地址。
但有什么办法,我可以得到从它的国名国code在android系统?
But is there any way I can get the country code from its country name in android ?
也请让我知道我是否有更有效的方法。
Also please let me me know if there is more efficient way.
感谢:)
推荐答案
您可以通过 getResources()。getConfiguration获得ISO 3166-1α-2(美国,英国等),()。 locale.toString()。分裂(_)[1]
。
这篇关于获取基于用户当前位置的Android国家code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!