Java Code Examples for android.location.LocationManager#isLocationEnabled()

The following examples show how to use android.location.LocationManager#isLocationEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidLocationService.java    From GeometricWeather with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static boolean locationEnabled(Context context, @NonNull LocationManager manager) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        if (!manager.isLocationEnabled()) {
            return false;
        }
    } else {
        int locationMode = -1;
        try {
            locationMode = Settings.Secure.getInt(
                    context.getContentResolver(), Settings.Secure.LOCATION_MODE);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        if (locationMode == Settings.Secure.LOCATION_MODE_OFF) {
            return false;
        }
    }

    return manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
            || manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 2
Source File: RNDeviceModule.java    From react-native-device-info with MIT License 6 votes vote down vote up
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean isLocationEnabledSync() {
  boolean locationEnabled;

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    LocationManager mLocationManager = (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    try {
      locationEnabled = mLocationManager.isLocationEnabled();
    } catch (Exception e) {
      System.err.println("Unable to determine if location enabled. LocationManager was null");
      return false;
    }
  } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    int locationMode = Settings.Secure.getInt(getReactApplicationContext().getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
    locationEnabled = locationMode != Settings.Secure.LOCATION_MODE_OFF;
  } else {
    String locationProviders = getString(getReactApplicationContext().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    locationEnabled = !TextUtils.isEmpty(locationProviders);
  }

  return locationEnabled;
}
 
Example 3
Source File: ActionIntroActivity.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    if (currentType == ACTION_TYPE_NEARBY_LOCATION_ENABLED) {
        boolean enabled = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
            enabled = lm.isLocationEnabled();
        } else if (Build.VERSION.SDK_INT >= 19) {
            try {
                int mode = Settings.Secure.getInt(ApplicationLoader.applicationContext.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
                enabled = (mode != Settings.Secure.LOCATION_MODE_OFF);
            } catch (Throwable e) {
                FileLog.e(e);
            }
        }
        if (enabled) {
            presentFragment(new PeopleNearbyActivity(), true);
        }
    }
}
 
Example 4
Source File: ActionIntroActivity.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    if (currentType == ACTION_TYPE_NEARBY_LOCATION_ENABLED) {
        boolean enabled = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
            enabled = lm.isLocationEnabled();
        } else if (Build.VERSION.SDK_INT >= 19) {
            try {
                int mode = Settings.Secure.getInt(ApplicationLoader.applicationContext.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
                enabled = (mode != Settings.Secure.LOCATION_MODE_OFF);
            } catch (Throwable e) {
                FileLog.e(e);
            }
        }
        if (enabled) {
            presentFragment(new PeopleNearbyActivity(), true);
        }
    }
}
 
Example 5
Source File: AbstractScanner.java    From easyble-x with Apache License 2.0 5 votes vote down vote up
private boolean isLocationEnabled(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return locationManager != null && locationManager.isLocationEnabled();
    } else {
        try {
            int locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
            return locationMode != Settings.Secure.LOCATION_MODE_OFF;
        } catch (Settings.SettingNotFoundException e) {
            return false;
        }
    }
}
 
Example 6
Source File: GpsUtils.java    From TowerCollector with Mozilla Public License 2.0 5 votes vote down vote up
public static boolean isGpsEnabled(Context context) {
    if (hasGpsPermissions(context)) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean isEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            return isEnabled && locationManager.isLocationEnabled();
        }
        return isEnabled;
    }
    // to cover the case when permission denied on API lower than 21
    return false;
}
 
Example 7
Source File: SystemPermission.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.P)
private boolean isLocationEnabledAndroidP(@NonNull LocationManager locationManager) {
    try {
        return locationManager.isLocationEnabled();
    } catch (Exception e) {
        return false;
    }
}