Android 正在检查全局位置访问权限
在Android中,我可以检查我的应用程序是否具有清单请求的位置权限,如下所示:Android 正在检查全局位置访问权限,android,permissions,location,Android,Permissions,Location,在Android中,我可以检查我的应用程序是否具有清单请求的位置权限,如下所示: if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // show alert } 但当用户进入Android设置>安全和隐私>位置访问并全局关闭时,该警报不会显示。是否有方法检测全局权限是否设置为off?
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// show alert
}
但当用户进入Android设置>安全和隐私>位置访问并全局关闭时,该警报不会显示。是否有方法检测全局权限是否设置为off?如果启用了全局位置,此函数将返回
public boolean isLocationEnabled() {
int locationMode = Settings.Secure.LOCATION_MODE_OFF;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.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);
}
}
谢谢,正是我想要的。现在我只需要弄清楚如何添加对API级别>=28的支持,因为Settings.Secure.LOCATION\u MODE\u OFF已被弃用。