0

我已经尝试了以下代码来获取国家代码名称

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

但我想如果我想获取用户所在的当前国家代码,这不起作用。

例如,如果用户从一个国家旅行到另一个国家,我想获取他所在的国家/地区代码。

我有一个获取用户当前国家代码的解决方案,但我被卡住了:

  1. 我获取用户当前位置。
  2. 使用当前位置获取用户地址,我可以从中获取国家名称。

但是有什么办法可以从 android 中的国家名称中获取国家代码?

另外请让我知道是否有更有效的方法。

谢谢 :)

4

2 回答 2

0

您可以通过getResources().getConfiguration().locale.toString().split("_")[1].

于 2014-08-11T11:00:08.203 回答
0

对于将来遇到此问题的任何人。使用智能定位

科特林:

SmartLocation.with(this).location(LocationGooglePlayServicesProvider()).oneFix().start {
            SmartLocation.with(this).geocoding().reverse(it, OnReverseGeocodingListener { location, mutableList ->
                val countryCode = mutableList[0].countryCode
            })
        }
于 2019-06-25T02:39:06.230 回答