检查全球位置访问权限

Checking global location access permission

在 Android 中,我可以像这样检查我的应用程序是否具有清单请求的位置权限:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  // show alert
}

但是当用户转到 "Android Settings" > "Security & Privacy" > "Location access" 并将其全局关闭时,该警报不会显示。有没有办法检测该全局权限是否设置为关闭?

此功能returns 如果启用了全局定位

   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);
        }
    }