Android移动实际上并一清二楚,它在哪里 - 但有通过类似的国家代码检索国家的一种方式?
无需了解确切的GPS位置 - 这个国家就足够了
我首先考虑使用时区,但实际上我需要更多的信息,因为如果位置是纽约或利马它会有所不同.
问题的背景:我有一个使用温度值的应用程序,我想将默认单位设置为Celsius或Fahrenheit,具体取决于位置是美国还是外部
获取国家/地区代码的最佳方式是什么?截至目前,我知道两种方法是通过TelephonyManager和另一种方法获取Locale,这是另一种在android中查找国家/地区代码的最佳和独特方式.
由于GDPR,我需要检查用户位置是否用户来自欧盟.直到现在我找到了这些解决方案 -
1)使用电话的语言配置获取国家(如果用户使用英语美国版本可能会误导,即使他可能来自其他国家/地区).
String locale = context.getResources().getConfiguration().locale.getCountry();
Run Code Online (Sandbox Code Playgroud)
2)使用GPS获取位置(需要位置Corse权限,我不想添加,特别是因为Android 6.0+运行时权限).
private void getCountryCode(final Location location) {
Helper.log(TAG, "getCountryCode");
AsyncTask<Void, Void, String> countryCodeTask = new AsyncTask<Void, Void, String>() {
final float latitude = (float) location.getLatitude();
final float longitude = (float) location.getLongitude();
// Helper.log(TAG, "latitude: " +latitude);
List<Address> addresses = null;
Geocoder gcd = new Geocoder(mContext);
String code = null;
@Override
protected String doInBackground(Void... params) {
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
code = …Run Code Online (Sandbox Code Playgroud) 我需要在下载我的应用程序时获取 PlayStore 设置的国家/地区(在应用程序中)。我只需要在从 Playstore 下载后安装我的应用程序时首次启动时获取此值。
如果没有,是否有办法从通过 AccountManager 检索到的帐户中获取国家/地区?
我已经四处搜寻,但尚未找到解决方案。非常感谢您的帮助!:)
我正在创建一个应用程序,该应用程序将根据您的移动网络提供商所在的国家/地区显示该国家/地区的所有替代移动网络提供商的列表。为此,我正在使用telephonyManager.getSimCountryIso()检索国家/地区代码。
官方的android开发人员文档说:“返回与SIM卡提供商的国家/地区代码等效的ISO国家/地区代码”,因此从此我希望国家/地区代码始终始终与设备位置无关。但这不是它实际的工作方式! 例如,我最近遇到这种情况:我有一个带有西班牙SIM卡的android设备,属于西班牙网络提供商。因此,如果我在西班牙,TelephonyManager.getSimCountryIso()将返回“ es”。一切正常。问题例如,当我去法国旅行时,我调试了应用程序,发现telephonyManager.getSimCountryIso()返回的国家代码为:“ nl”(来自荷兰! 。与西班牙使用的设备和SIM卡相同,因此国家/地区代码ISO仍应为“ es”。
我的问题是这种方法实际上如何工作?如果我使用西班牙SIM卡,为什么会收到国家代码“ nl”(荷兰)?
预先感谢您的任何帮助
我想在不使用SIM卡的情况下获取设备国家/地区代码。
例如,如果国家/地区:斯里兰卡国家/地区代码:LK
我的最终目标是获取调用代码(斯里兰卡为+94)
我尝试过
TelephonyManager tm = (TelephonyManager)this.getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
Run Code Online (Sandbox Code Playgroud)
但这返回空字符串
如何在android代码中获取我的SIM的国家代码.我用过
TelephonyManager tm = (TelephonyManager)getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();
Run Code Online (Sandbox Code Playgroud)
但在这里,我为孟加拉国获得了像"BD"这样的国家名称,我需要为孟加拉国提供+880.这段代码的工作原理与此类似.
Locale.getDefault().getCountry();我需要代码为+ 91代表ind,+ 880代表bd.
我正在使用Firebase身份验证与谷歌登录和Facebook登录
有没有其他方法可以知道用户来自哪个国家/地区?
在应用程序知道用户来自哪个国家/地区之后,应用程序将决定向用户显示哪个服务/未来.
public static Country getCountryFromLocation(Context cxt, List<Country> countryList, Location location, int maxAddress) {
if (location == null || countryList == null || countryList.isEmpty()) {
Log.d("ooo", "location == null || countryList == null || countryList.isEmpty()");
return null;
}
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder coder = new Geocoder(cxt);
try {
List<Address> addressList = coder.getFromLocation(latitude, longitude, maxAddress);
for (Address address : addressList) {
String curCountryCode = address.getCountryCode();
Log.d("ooo", "address " + address.toString());
if (TextUtils.isEmpty(curCountryCode)) {
continue;
}
for (Country country …Run Code Online (Sandbox Code Playgroud) android google-maps country-codes geolocation mobile-country-code
android ×9
geolocation ×3
location ×2
android-gps ×1
google-maps ×1
google-play ×1
java ×1
localization ×1
privacy ×1
sdk ×1