问题
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.
回答1:
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;
}
回答2:
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.
回答3:
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.
回答4:
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
}
来源:https://stackoverflow.com/questions/16862987/android-check-location-services-enabled-with-play-services-location-api