我正在开发一款使用 Google Play 服务的应用。在某些手机上,客户端会为位置返回 null。发生这种情况是因为 locaiton 选项未在 google 设置上启用,如所附图片所示。
如何以编程方式检查 Android 应用程序中是否启用了谷歌设置位置?
http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/
最佳答案
可以检查 google play 服务本身是否可用。按照此处的说明进行操作: https://developer.android.com/training/location/retrieve-current.html
即使启用了 Google Play Services 定位服务,getLastLocation() 也有可能返回 null。例如,在重新启用它们之后。但是,您可以使用以下 Intent 将您的用户带到 Google Play 服务位置设置:
Intent settings = new Intent("com.google.android.gms.location.settings.GOOGLE_LOCATION_SETTINGS");
startActivity(settings);
最后,还可以检查 Android 位置服务是否已启用:
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
关于Android:检查谷歌设置位置是否已启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24067256/