Jon*_*Jon 2 android country-codes android-location android-permissions android-6.0-marshmallow
我想获取用户的国家/地区代码,而不使用在 android M 下需要主动请求的任何权限。例如,如果用户位于中国,我想取回 CH。我不想为此使用“危险”权限 - 也不想read_phone_state通过TelephonyManager,也不想通过位置来尝试根据他的 GPS 进行猜测。有没有办法做到这一点,如果有的话,怎么做?
我尝试使用以下代码,但如果用户将其语言设置与其所在国家/地区分开(例如印度的用户将其手机设置为 English_US),则很容易失败:
Resources r = paramContext.getResources();
Configuration config = r.getConfiguration();
Locale currentLocale = config.locale;
Log.d("TAG", "Country: " + currentLocale.getCountry());
Run Code Online (Sandbox Code Playgroud)
所有允许以一定置信度确定用户当前位置的方法都被标记为危险的。
话虽这么说,有几种方法不太可靠,但可能对您来说可以接受:
TelephonyManager.getSimCountryIso()
| 归档时间: |
|
| 查看次数: |
1466 次 |
| 最近记录: |