Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在检查全局位置访问权限_Android_Permissions_Location - Fatal编程技术网

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?

在Android中,我可以检查我的应用程序是否具有清单请求的位置权限,如下所示:

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已被弃用。