25

You can determine if any providers are available with LocationManager, but can it be done with the google play services location api?

When nothing is enabled, in fact "access to my location" is off, all the API calls (connect(), requestLocationUpdates()) succeed, but you never get an onLocationChanged().

Seems silly to have to use both LocationManager and the LocationClient.

I guess what I need is some way to know that onLocationChanged(0 will never get called.

user1248322
  • 723
  • 2
  • 7
  • 10

4 Answers4

26

I've combed the docs and also haven't found any way to use LocationClient to detect if location services are enabled. onConnected, onDisconnected, and onConnectionFailed do not seem to be linked to whether Location Services are enabled or not.

I'm currently doing location requests using LocationClient, but using the old locationManager.isProviderEnabled(String provider) method to detect if the Location Services are enabled. This is optimal for me, as, even if LocationClient did provide a way, it makes no distinction between GPS and Network and I'd really like to be able to request that the user enable GPS.

if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
    //GPS Provider disabled
    return false;
}
Kris Pena
  • 487
  • 1
  • 5
  • 9
  • I find this very odd too! – Bear Feb 03 '14 at 17:49
  • 5
    `if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {}` I think is the most complete approach. – Diolor Jul 31 '14 at 17:23
12

There is now a reliable way to detect if location updates are available since Google Play Services 7.3.0:

Implement and register a LocationCallback and override onLocationAvailability() or call LocationServices.FusedLocationApi.getLocationAvailability() to retrieve the availability status. If it's false, then you known the location updates aren't going to be delivered unless something changes in the device configuration.

This method is actually quite smart: for example it is able to determine that no location updates are going to be delivered on a WiFi-only device if WiFi is disabled or device is in airplane mode, even if the Network location provider is always enabled.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • Doc says: Returns the availability of location data. When isLocationAvailable() returns true, then the location returned by getLastLocation(GoogleApiClient) will be reasonably up to date within the hints specified by the active LocationRequests. To my understand that show if there is some location data is currently available, not if the location services are enabled or not. – lujop Aug 16 '15 at 07:44
  • This methods only notifies that location will not be delivered, but it does not tell reason, while it may return false because of many reasons. – B-GangsteR Sep 13 '18 at 07:57
  • You can use SettingsClient to determine in advance if a location request will succeed and get more information about the unavailability: https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient – BladeCoder Sep 14 '18 at 12:24
4

Google Play Services 7.0 introduced a Settins Api where you can ask for location settings you need and then query if are enabled or ask the user to enable them.

lujop
  • 13,504
  • 9
  • 62
  • 95
0

This works for me. Tested on an Amazon tablet device vs a Samsung s7

 if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
            //Google Play services are available
    } else {
            //Google Play services are not available on this device
    }
Ch Vas
  • 993
  • 8
  • 10