android check location services enabled with play services location api - android

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.

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;
}

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.

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.

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
}

Related

Getting last known location from Google Play Services while device location is off

I'm using FusedLocationApi to get device's last known location from Google Play Services.
My understanding is that getLastLocation delivers the last known location (assuming the needed permissions are granted) regardless of which app has requested the location even when the device location is off but does not work as such.
When the location is on it works as expected; But when it's off it returns null though it has previously retrieved the location for example from the Google Maps app.
Should it work like this or something's amiss here?
When you turn off a location provider on the device (GPS, Network, etc), it clears the last known location. Any apps that still shows a point of some kind when location is turned off probably cached it.
So the answer is that you should receive null for getLastLocation if location services is turned off.

Is Fused Location Provider good choice?

I am developing an application where I want to use Fused Location Provider. But I have some doubts, and couple of questions.
When GPS is off and I set priority to HIGH, does that mean that the GPS will be automatically turned on, or not?
Can I set UpdateLocation with Fused provider with HIGH priority on demand to save battery at least a little bit?
How can I know what Fused provider is using (is it a GPS or a network provider)?
And finally
Is Fused provider really the best choice for android location? Are there any negative points about it?
What is your opinion?
Thanks in advance.
When GPS is off and I set priority to HIGH, does that mean that GPS will be automatically turned on, or not?
No, it will not be turned on automatically. But if you use SettingsApi, will prompt a dialog to user and gives information that GPS is must be turned on. If user accepts it, the gps will be active automatically. Check the SettingsApi
How can I know what Fused provider is using (is it a GPS or a network provider)
If you use fused provider api with SettingsApi properly. It will make adequate the required settings for current location request.
Is Fused provider really the best choice for android location? Are there any negative points about it?
In my opinion, before fused provider you must deal with directly providers(Gps, network) But fused just asks you, "how accurate locations you wanna receive ?"
As in here https://developer.android.com/training/location/index.html stated very clearly that, the Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible. So I hope you got your answer.
I made a testing application for Gps, Wifi and Fused Location Provider and testing it for 2 days. It's better because it uses both of them and most of the time it's the one most accurate. Also, Gps data is a very noisy data that causes jittering, to solve this low-pass filter or other filters are used. One of the most successful filter used to get most accurate results is Kalman Filter. FusedLocationProvider use this filter same as RotationVector which is a fused sensor combines hardware and software. RotationVector uses accelerometer, gyroscope(if available), and magnetic field sensor to get and filter positition and azimuth data.
Location.getProvider for Gps with LocationManager returns "gps", Wifi returns "network", and FusedLocationProvider returns "fused".
When GPS is off and I set priority to HIGH, does that mean that the GPS will be automatically turned on, or not
Anything other than "Battery Saving" turns Gps on if available. This settings available on my Android 7.1.1 phone. Setting for location was different on previous versions of Android on user's side. As a developer to enable using Gps you should set mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
PRIORITY_HIGH_ACCURACY - Use this setting to request the most precise location possible. With this setting, the location services are more likely to use GPS to determine the location.
Setting Priority also determines battery use level too.
Can I set UpdateLocation with Fused provider with HIGH priority on demand to save battery at least a little bit?
Yes, you can set interval of location request in addition to priority.
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
How can I know what Fused provider is using (is it a GPS or a network provider)?
Location from Wifi never returns true for Location.hasSpeed() but Gps returns almost always true if you are outdoors. Also location.getExtras() have satellites tag which you can check for satellites which is only available for Gps. Speed may not be correct if you are walking or as far i've read so far, i haven't tried this on car, when speed it less than 5km/h it's not very accurate. I mean if you are using FLP and last location data contains speed info it's definitely from Gps.
Are there any negative points about it?
As of Android 8.0 and above there is location retrieving limit if you do not use a Foreground Service or get location on foreground while app is not paused for both FLP and LocationManager.
Also FLP requires GooglePlayService to be available on user's device and it should be above a particular version. 10 or 11 depending on which one you use. This can be trouble if you wish to publish your apps on a country, for example China, that bans Google Play Services.
The existing answers don't say why the FusedLocationProvider is better.
It is better because the API fuses from more data sources (sensors, wifi, context, history) in an intelligent and battery-saving way. Also, Google is always improving it by adding more data sources. If your app uses it, you get those improvements for free.

Google Maps Api v2 - Detect gps activation

I want to locate user using best provider enabled.
When I launch my app with GPS off, the location manager will use network to get user location.
If I turn on my GPS while the app still running, the GPS will not be used because the location manager is listening network provider.
How can I detect the activation/desactivation of GPS and update user location on the map while the location manager is using the network provider ?
SOLUTION FOUND
Use locationclient, see this code example!
Use this line to detect if the GPS was enabled by the user:
yourLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
It will return true if it's enabled or ortherwise false.
But as MaciejGórski said, do yourself a favor and use the LocationClient. You will never ask yourself questions like this anymore.
Use new LocationClient fused provider. It does all the hard work for you and listens for all providers when they are enabled.

Checking GPS status with Google Play Services

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?
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.

How do these Android location options affect the LocationManager isProividerEnabled method?

I'm trying to figure out the location services options under Android 2.3.3 on my Verizon Droid X, I have 3 options:
Google location services
Standalone GPS services
VZW location services
Enabling or disabling Google location services causes the following to return true or false respectivly.
myLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
Enabling or disabling Standalone GPS services causes the following to return true or false respectivly.
myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
Enabling or disabling VZW location services seems to have no affect on the isProviderEnabled method. If this option is the only one enabled, then the isProviderEnabled method always returns false regardless if using NETWORK_PROVIDER or GPS_PROVIDER.
The way I understand this is:
Google location services = WiFi MACID location
Standalone GPS services = GPS location
VZW location services = nothing except something special to Verizon (like Navigator)
Is this assumption correct? If so, where does AGPS and CellID come into play?
"Each kind of location service is being used not only to help their applications bring you the most relavent information, but to assist the network in improvements based on your individual experience.
Google wants to know where you are to answer your questions of 'Coffee shops near my location' and various other things.
Verizon uses your path from tower to tower to tower as you travel to make someone else's (or your) trips through the same area more efficient. If while driving up a highway you are on tower A and tower B, C, and D are all coming in to range, but your phone always goes for C...in the future, tower A can just tell your phone to look for tower C rather than have it search blindly.
There's a lot more detail behind the scenes, but that's the general idea."
see reference link below for more details
http://www.droidforums.net/forum/motorola-droid-x2/159360-location-security-settings-questions.html

Categories

Resources