12

I have been working and testing on background location updates using GoogleApiClient for Interval and Displacement based updates. On testing and analyzing for a few days I figured out some variations in the output which I did not expect.

  1. While using setInterval and setFastestInterval for Interval based updates, say I have set an Interval as 15 mins and Fastest Interval as 10 mins, 90% of the time I am getting updates in the expected Interval (10 to 15 mins). But sometimes I noticed that the updates take much longer time than the specified Interval, like, the difference is around 30 mins and 60 mins. Any Idea about why is the difference?

  2. While using setMinimumDisplacement for Distance based updates, say I have set a Displacement as 200 meters, I am getting updates only on stationary points(While travelling it doesn't give updates even if it is more than 200 meters) which are 200 meters and above. Is this how it works normally?

I am using PendingIntent type of location requests in order to receive location updates in BroadcastReceiver for location updates in background.

fusedLocationProviderClient.requestLocationUpdates(locationRequest, pendingIntent);

While testing, the Location Services were ON and Location Mode was HIGH_ACCURACY.

Joshua
  • 1,167
  • 1
  • 14
  • 32
  • 3
    I can't say normal behavior but I have seen issues similar to yours. Truthfully, the answer is what @Mitesh Vanaliya has quoted from documentation, **All location requests are considered hints, and you may receive locations that are more/less accurate, and faster/slower than requested**. Or, to paraphrase, "You can tell us what you want, and we'll do that sometimes, but in the end we'll do what and when we want" – Gary99 Jan 18 '18 at 15:04
  • If you want to not use Google's recommended `FusedLocationApi`, you can use the older LocationManager to get more control for yourself. [Here](https://developer.android.com/guide/topics/location/strategies.html) is some info on how to do that. – Gary99 Jan 18 '18 at 15:09
  • I didn't want to go for `LocationListener` because it will get terminated when the app is terminated. I want to get location updates even if the app is terminated, which is why I went for `PendingIntent` version of `FusedLocationProviderClient`'s `requestLocationUpdates`. The updates which I get from `setInterval` is far more better than `setMinimumDisplacement` since locations from _setInterval_ is more accurate compared to _setMinimumDisplacement_. – Joshua Jan 19 '18 at 07:28
  • Since I have some processes to be done based on the Distance the user travels, I am expecting it to give location updates atleast with the accuracy of 1km. But the problem with _setMinimumDisplacement_ is, its giving updates only when the user stops somewhere. When the user is traveling continuously I am not getting a single update even if it crosses the specified MinimumDisplacement range. Is there any other way or implementation to make sure I get location updates based on specified distance atleast approximately? – Joshua Jan 19 '18 at 07:29
  • I do had similar issues with my trials. Check out my question and answers from fellow SO https://stackoverflow.com/questions/35745924/location-from-gps-provider-or-network-provider-in-android-is-not-consistent – Sreehari Jan 23 '18 at 07:50
  • @Stallion I think the issue you have is normal for the code and api you have used. As per the answer you have marked as the best, accelerometer, gyrometer and magnetometer based data is the best way to get more accurate data. As per [this](https://developer.android.com/reference/android/location/Location.html#getAccuracy()) documentation, its accuracy is around 68% only. But in my case, the `setMinimumDisplacement` is not even near approximate. I have explained it in **second point** in the question. – Joshua Jan 23 '18 at 17:50

2 Answers2

4

Please refer to the documentation to get the proper behavior of LocationRequest API.

LocationRequest Api documentation

From this documentation:

  • Applications cannot specify the exact location sources, such as GPS, that are used by the LocationClient. In fact, the system may have multiple location sources (providers) running and may fuse the results from several sources into a single Location object
  • Location requests from applications with ACCESS_COARSE_LOCATION and not ACCESS_FINE_LOCATION will be automatically throttled to a slower interval, and the location object will be obfuscated to only show a coarse level of accuracy.
  • All location requests are considered hints, and you may receive locations that are more/less accurate, and faster/slower than requested

For more detail description read full documentation from above link.

Hope this explanation may help you.

Mitesh Vanaliya
  • 2,491
  • 24
  • 39
  • 1
    Thanks for the explanation. I already know some information about the location sources and accuracy. I am expecting some more information about the **second point** in the question, like, is it how it normally works? or do I have to implement some more code for it. – Joshua Jan 17 '18 at 10:54
0

I have found the answer to my second question. Documentation says that it's not recommended to setMinimumDisplacement to 0 but that's the actual trick. It works as expected when it's set to 0.

It's works correctly when having two different LocationRequests (Based on Interval and Displacement) so that one's setting does not affect the other.

A Foreground Service is preferred for the above scenario so that the location updates doesn't get killed by the OS.

Joshua
  • 1,167
  • 1
  • 14
  • 32