Checking GPS status with Google Play Services
Asked Answered
I

1

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?

Inconsistent answered 15/6, 2013 at 8:56 Comment(1)
with GPS you mean the Location switch in System -> Settings?Bloodstock
W
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.

Wednesday answered 15/6, 2013 at 10:40 Comment(9)
So we can't actually get rid of LocationManager and it will used for such things?Inconsistent
@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.Wednesday
@Wednesday - 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) ?Cloudlet
@raj: Call isProviderEnabled() on LocationManager.Wednesday
@Wednesday 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 ?Cloudlet
@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.Wednesday
@Wednesday 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.Cloudlet
@Wednesday 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 Toastasking "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 ?Cloudlet
@Cloudlet 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 developer.android.com/training/location/location-testing.htmlNonobservance

© 2022 - 2024 — McMap. All rights reserved.