Search code examples
androidgoogle-play-servicesandroid-location

FusedLocationProvider - any way to get a location if location mode is "Device Only"?


I've been playing around with FusedLocationProvider and I found that if your phone's Location Mode is set to "Device Only" (changed in Settings - it means only GPS is enabled for location. Wifi networks and cell towers aren't used to improve accuracy), then I couldn't find a way for FusedLocationProvider to get you a location without asking the user to change it to High Accuracy (onResolutionRequired is always called).

All four LocationRequest priorities did not work:

  1. PRIORITY_HIGH_ACCURACY - asked to switch to Location Mode "High Accuracy"
  2. PRIORITY_BALANCED_POWER_ACCURACY - same as above
  3. PRIORITY_LOW_POWER - same as above
  4. PRIORITY_NO_POWER - just didn't work

Is there no way to get location through FusedLocationProvider if the device is in this mode? I am pretty sure you can get it through the android.location.LocationManager. That seems like a huge design flaw if this is meant to be better abstraction on top of it..


Solution

  • You do need to use LocationManager to detect this, and I agree that it's really dumb. Use the following code:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
       requestLocationUpdates();
    } else {
       //Location request code
    }
    

    Here's a sample project I've made that incorporates this technique:

    https://github.com/gavingt/basic_location_improved