user1195883
user1195883

Reputation: 824

Checking global location access permission

In Android I can check if my app has location permission as requested by the Manifest like this:

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

But that alert is not showing when the user goes to "Android Settings" > "Security & Privacy" > "Location access" and turns it to off globally. Is there a way to detect if that global permission is set to off?

Upvotes: 1

Views: 331

Answers (1)

Ferran
Ferran

Reputation: 1448

This function returns if Global Location is enabled

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

Upvotes: 2

Related Questions