I am getting location in background through Fused Location Provider but always getting null Location.
Can you please help me
Are you using an android emulator?
The fused location provider manages the underlying location technologies, such as GPS and Wi-Fi, and provides a simple API that you can use to specify the required quality of service. For example, you can request the most accurate data available, or the best accuracy possible with no additional power consumption.
well, it happened to me before when I used some android emulators with low API levels (Lollipop L25). so the emulator doesn't have any location info to return
but it will work on a physical device.
now if your problem just in the background what background service are you using
can you share some code?
Related
I have a real quick question. As far as I know the Geolocator package uses the Android FusedLocationProviderClient to access the users location. Does this client use all the permitted opportunities to get the location (like gps,wlan,mobile networks...) or does this only use the gps data?
All together: How does the FusedLocationProviderClient get the data for the users location?
Thanks for the answer.
The fused location provider manages the underlying location technologies, such as GPS and Wi-Fi, and provides a simple API that you can use to specify the required quality of service. For example, you can request the most accurate data available, or the best accuracy possible with no additional power consumption
We need not explicitly choose either GPS or Network location Provider, as the “Fused Location Provider” automatically chooses the underlying technology and gives the best location as per the need.
REF : https://developers.google.com/location-context/fused-location-provider/
So, regaring you question about the accuracy of the location it totally depends whethere the location was enabled and the fusedLocation stored it for furture use.
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.
I am using fused location provider from the new GooglePlay API(LocationClient class). But the method Location.GetSpeed() is not returning correct speed (shows 0). I read that I have to use LocationManager class to get the speed, but when I tried LocationManager a new problem occured. The location manager NetworkProvider is not working, here is a thread with the problem http://code.google.com/p/android/issues/detail?id=57707 .
So what is the best way to get the speed of movement. I tried calculating it distance/time but it is not accurate..
The only sensor that delivers speed is GPS.
Wlan and GSm cell Tower locatining cannot deliver speed.
If you need speed, you have to make sure that GPS is used exlusivley as Location Provider.
You cannot use NetworkProvider for that task.
If speed is an important requirement, which has higher priority than battery consumption you have to stay with GPS Provider only. You have to avoid the fused location provider.
Its technically not possible to have speed without GPS.
I also encountered this problem when I was using Google Play Location API,
I hope this can help.
It returns 0 because your device cannot get a lock on the GPS, or cannot connect to the GPS.
I tried to get the speed using an older lenovo device and it returns 0 because it cannot lock on a gps.
I tried using a samsung galaxy nexus and it returned my speed(has a better GPS sensor).
The GPS sensor in your phone might not be good or you are in an area that has a weak GPS signal such as inside a house or building.
Even if you use the locationManager to get the speed if you are not connected to the gps, your getSpeed will still return 0.
In my application I would like to determine the user's current location. I do however have a couple of questions in this regard:
There are different Location Providers, which one is the most accurate? The GPS Provider or the Network Provider?
In how far do those available provider differ? How do they function?
Could you please provide me with some code-snippets or tutorials on how to get started with implementing GPS functionality in my application?
There are 3 location providers in Android.
They are:
gps –> (GPS, AGPS): Name of the GPS location provider. This
provider determines location using satellites. Depending on
conditions, this provider may take a while to return a location fix.
Requires the permission android.permission.ACCESS_FINE_LOCATION.
network –> (AGPS, CellID, WiFi MACID): Name of the network
location provider. This provider determines location based on
availability of cell tower and WiFi access points. Results are
retrieved by means of a network lookup. Requires either of the
permissions android.permission.ACCESS_COARSE_LOCATION or
android.permission.ACCESS_FINE_LOCATION.
passive –> (CellID, WiFi MACID): A special location provider for
receiving locations without actually initiating a location fix. This
provider can be used to passively receive location updates when other
applications or services request them without actually requesting the
locations yourself. This provider will return locations generated by
other providers. Requires the permission
android.permission.ACCESS_FINE_LOCATION, although if the GPS is not
enabled this provider might only return coarse fixes. This is what
Android calls these location providers, however, the underlying
technologies to make this stuff work is mapped to the specific set of
hardware and telco provided capabilities (network service).
The best way is to use the “network” or “passive” provider first,
and then fallback on “gps”, and depending on the task, switch between
providers. This covers all cases, and provides a lowest common
denominator service (in the worst case) and great service (in the best
case).
Article Reference : Android Location Providers - gps, network, passive By Nazmul Idris
Code Reference : https://stackoverflow.com/a/3145655/28557
-----------------------Update-----------------------
Now Android have Fused location provider
The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs. It simplifies ways for apps to get the user’s current location with improved accuracy and lower power usage
Fused location provider provide three ways to fetch location
Last Location: Use when you want to know current location once.
Request Location using Listener: Use when application is on screen / frontend and require continues location.
Request Location using Pending Intent: Use when application in background and require continues location.
References :
Official site : http://developer.android.com/google/play-services/location.html
Fused location provider example:
GIT : https://github.com/kpbird/fused-location-provider-example
http://blog.lemberg.co.uk/fused-location-provider
--------------------------------------------------------
GPS is generally more accurate than network but sometimes GPS is not available, therefore you might need to switch between the two.
A good start might be to look at the android dev site. They had a section dedicated to determining user location and it has all the code samples you need.
http://developer.android.com/guide/topics/location/obtaining-user-location.html
There are some great answers mentioned here. Another approach you could take would be to use some free SDKs available online like Atooma, tranql and Neura, that can be integrated with your Android application (it takes less than 20 min to integrate).
Along with giving you the accurate location of your user, it can also give you good insights about your user’s activities. Also, some of them consume less than 1% of your battery
Before I go into location based mode I check for existence of any location providers by calling
List<String> android.location.LocationManager.getProviders(boolean enabledOnly) //enabledOnly = true
and checking the size of the resulting list.
Now I tested my App on a HTC Desire with Android 2.2. The system settings don't allow any location tracking (GPS and mobile is turned off).
However, the list get returned has 1 entry, whose value is "passive". What is it? Can I work with it? The provider seems to be slow / not working.
From the Android API reference:
A special location provider for receiving locations without actually initiating a location fix. This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers.
So no, it's not likely to do you any good if there are no other location providers available.