I have a service which is started on application start or phone boot.
Within the onCreate I define the parameters of requesting location updates:
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 8046, this);
My understanding is that it will only provide an update now if either 5 minutes has passed or the user has moved more than 5 miles. What I'm actually getting is an update every minute or less. The 300000 number being 5 minutes in miliseconds, and the 8046 being 5 miles in meters.
I know the service isn't getting called that often by any sort of mistake as it creates a toast when the service first starts and that's not displaying all the time.
Can anyone helps with this one?
From the document for requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent) at http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)
Prior to Jellybean, the minTime parameter was only a hint, and some location provider implementations ignored it. From Jellybean and onwards it is mandatory for Android compatible devices to observe both the minTime and minDistance parameters.
Probably, the provider also ignores the distance limit.
Related
locationManager.requestLocationUpdates(provider, 2500, 2, this);
You need to walk 2 meters in every 2.5seconds to request for new location?. Correct me if i am wrong. Thank you.
minTime – Wise choice of value for minTime will help you reduce
power consumption. What you need to keep in mind is that elapsed
time between location updates will never be less than minTime, but
it can be greater, because it is influenced by the implementation of
each given Provider and the update interval requested by other
running applications.
minDistance – Unlike minTime this parameter can be turned off by
setting it’s value to 0. However, if minDistance is set to a value
greater than 0, location provider will only send updates to your
application if location has changed at least by given distance. This
parameter is not a great power saver like minTime, but it should be
kept in mind although.
These two parameters work in AND relation, so in order to receive location update, both of their conditions need to be achieved (i.e. more then 5 seconds has passed, and the distance change is greater than 10 meters).
You can check this link:
https://blog.codecentric.de/en/2014/05/android-gps-positioning-location-strategies/
I need to get GPS location updates every minute (battery life isn't a problem as the device(s) will be charged within the vehicles). It's a company delivery app, with vehicle tracking, used in conjunction with Google maps, to track journeys for delivery planners etc.
I've used both AlarmManager and LocationManager.requestLocationUpdates() with a listener. The later seems a bit ropey and going by the documentation, it may or may not give a location. But I've seen that even though the minTime is set to 1 minute and distance is 0/1, I'm still only seeing updates once or twice per 5 min period.
private void startGeoLocationSender()
{
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new GeoLocationListener(this);
// MINIMUM TIME TO REQUEST , MIN DISTANCE TO REQUEST, LISTENER
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
AppSettings.COLLECTOR_PROC_1_MINS,
AppSettings.COLLECTOR_DISTANCE_1_M, locationListener);
}
So, is it worth me just going back to an AlarmManager and forcing an update manually for it to be reliable? Or is there another method, which is 100% reliable given a specific update time? What is better suited to my requirement?
PS. I'm very aware of the other '000s of topics on the subject, but I've never seen such a Good Vs Evil trend. Some folk swear by location listeners, and others only AlarmManagers.
I am not sure of a better way, so I would recommend AlarmManager.
If you use only LocationListener you can't ensure that will receive update in interval time needed.
I really think that the best option is the AlarmManager or a Scheluder Service with a trigger to your LocationListener.
I'm creating an application (For educational purposes) which records the user's location every 30 minutes, and enables the user the view all the locations on the map. I don't want updates to be more frequent than 30 minutes, but they are.
This is how I call requestLocationUpdates:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 60 * 1000, 0, pe);
Now, the documentation clearly says:
The elapsed time between location updates will never be less than minTime
But I did see some answers here on SO stating differently (This answer, for example).
It seems like I'm getting updates whenever they are available from the GPS. The GPS icon never turns off and the updates rate becomes greater than 1 update/second. So I have 2 questions here:
The minTime parameter doesn't fulfill its purpose, not even as a hint (Yea, a hint to 30 minutes update rate leads to more than update a second...). What does it do, then?
Is there any other way to do it? I don't want the GPS to be enabled all the time because it will consume my battery too fast. Maybe I could schedule alarms repeating each 30 minutes, and call requestSingleUpdate?
The minTime parameter doesn't fulfill its purpose, not even as a hint (Yea, a hint to 30 minutes update rate leads to more than update a second...). What does it do, then?
From Jellybean onwards devices must observe the minTime parameter, so it does have a purpose (now).
Is there any other way to do it? I don't want the GPS to be enabled all the time because it will consume my battery too fast. Maybe I could schedule alarms repeating each 30 minutes, and call requestSingleUpdate?
Yes, use a Handler to request one update with requestSingleUpdate() every 30 minutes.
I've tackled this before in a previous question, let me know if the code in that answer helps and if you have any questions it doesn't address.
The second parameter, min distance difference in meters is set to zero, it causes constant updates.
Prefer using requestSingleUpdate within a timer+handler on a desired period in minutes.
In my application, I have calculated speed based on GPS locations.
As GPS receiver can have different coordinates even being on the same location, my app is recording speed even when phone is on my desk.
I need to avoid GPS data if I am receiving it being on same location.
I am thinking of adding some logic based on “bearing”, so that I can decide if the phone is actually moving or it’s receiving jumpy location data from any direction.
Before that I would like to know is there any other way to avoid this kind of data?
AS per the Javadoc for the method request public void requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent) found here
minTime minimum time interval between location updates, in
milliseconds minDistance
minimum distance between location updates, in
meters
so you can use minDistance parameter to avoid getting update for very short distances...
After working on many methods I programmed it with bearing.
I am getting speed based on last 5 locations. I added Logic to check bearing for each location with next location and if all bearing have larger difference, I assumed locations are being received from random directions and so the device is not actually moving.
And it worked great for me :)
Porting our location based game to Android.
We rely on updating user location when the app is in the background. What's the best way to accomplish this on the Android side while mitigating battery impact?
We don't need high-frequency updates, even 20 minute cadence is acceptable.
Thanks for any help
You will need to create a Background Service in order to accomplish what you want.
Inside that Background Service you will need to use LocationManager and request for Location Updates.
The function that request for Location Updates takes parameters for the fixes intervals as shown below:
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
you will use the minTime parameter to choose the interval between the fixes:
minTime : the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.