8

Google recommends now using Google Play Services to manage user location. But how can we, using this API, check wether GPS is turned on in device or if we still have gps connection?

In com.google.android.gms.location.LocationListener we have only one method abstract void onLocationChanged(Location location) which is called when the location has changed, but we don't know there anything about GPS status.

How to use Google Play Services like "old" LocationManager and LocationListener?

Kamil Lelonek
  • 14,592
  • 14
  • 66
  • 90

1 Answers1

10

But how can we, using this API, check wether GPS is turned on in device or if we still have gps connection?

You can't, AFAICT. I suppose the argument is that since LocationClient is blending data from several sources, there is no API to determine if any specific source is or is not being used.

You will need to use LocationManager if you want to determine whether GPS is enabled or not.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    So we can't actually get rid of `LocationManager` and it will used for such things? – Kamil Lelonek Jun 15 '13 at 13:46
  • 3
    @squixy: Anything you want from `LocationManager` that is not available via `LocationClient`, you will still need to use `LocationManager` for. Think of `LocationClient` as an add-on to `LocationManager`, not a replacement. – CommonsWare Jun 15 '13 at 13:56
  • @CommonsWare - it seems for LocationClient to return a non-null Location, the Google location settings has to be enabled too. Cannot find an api call to know this setting or an intent action to open this setting page. Any sample code using LocationManager to find out if Location access is enabled (along with WiFi and mobile network enabled) ? – raj Jul 14 '13 at 03:59
  • @raj: Call `isProviderEnabled()` on `LocationManager`. – CommonsWare Jul 14 '13 at 10:53
  • @CommonsWare thanks, that works(used it before) but I noticed that Location is null if "Google location settings" in Google plus settings is not enabled even if "Location access" is enabled, any thoughts on that ? – raj Jul 14 '13 at 19:45
  • @raj: Just to confirm, are you saying that if "Access location" in the Google Settings app is unchecked, that `LocationClient` consistently returns `null`? If that is what you mean, I have not tried that scenario. – CommonsWare Jul 14 '13 at 20:01
  • 2
    @CommonsWare If `Location access` is enabled but `Wi-Fi & mobile network location` is not enabled within it then Location is null. I tried with everything enabled in `Location access` and `Google location settings` disabled, the Location was null. (I doubled checked it right now and it seems to return a non-null `Location`, not sure if it is a cached/last known location from it provider. – raj Jul 14 '13 at 23:52
  • @CommonsWare in current google maps app, if `Google location settings` is disabled and you try to find your location a pop-up appears asking you to change google location settings or a `Toast`asking "Please enable Google apps location access". All of these are on a device, the emulator(4.2 w/ google api) always returns null no matter what, any idea on how to simulate location updates on emulator ? – raj Jul 14 '13 at 23:57
  • @raj Maybe try with the app in the market named Fake GPS. BE sure to enable the allow mock location. There is also an API for mock location in LocationClient check here for info http://developer.android.com/training/location/location-testing.html – Igor Čordaš Jun 19 '14 at 10:44