Hunt
Hunt

Reputation: 8435

need to sync onLocationChanged

i am creating one application which broadcast the location of a user over a web for that i have created a service in which i am using LocationListener and getting updates from onLocationChanged

i have set requestLocationUpdates as follows

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                                                15000,
                                                0, 
                                                geoHandler);

when ever i am getting an update in onLocationChanged i broadcast the location over web.

Now the problem is onLocationChanged gets fired too frequently even if it doesn't cross the time limit (that is totally unpredictable) and which in turns trigger my custom function which is broadcasting the location information too frequently.

so i dont know how would i sync this process in a proper timely manner like after every 15 seconds the location will be broadcasted over web ,rather then firing it up too frequently

Upvotes: 1

Views: 668

Answers (2)

locationManager.requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

minTime is minimum time interval between location updates, in milliseconds

minDistance is minimum distance between location updates, in meters

You are setting minTime as 15seconds but since your minDistance is 0 meters its triggered too frequently. It doesnt have to wait for 15 seconds to pass since a slight change in GPS value triggers a location update. Giving some meters (e.g. 1 or 3) shall increase the interval between updates.

Upvotes: 1

Tudor Luca
Tudor Luca

Reputation: 6419

How frequent is the onLocationChanged being called?

It is an odd behabiour because when you add a minTime!=0 parameter to :

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, geoHandler)

then the listener is not notified only after minTime.

After a deeper research I found that minTime is relative. It depends on many factors and therefore your onLocationChanged is not called precisely after minTime. Google recommands to use this parameter for power and battery efficiency. So you should use is as a bigger value, like 1000*60*5 (5 minutes.)

If you really need to broadcast your location on each 15 seconds you should bare in mind that GPS pinpoints can not be triggered instantly, it take at least 5-10 seconds (best case). Therefore you have to change your design to something like this:

  • locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, geoHandler)
  • in onLocationChanged just check when was the last time you sent/processed your location, and if the time elapsed is >= 15 sec, broadcast it;

Hope it helps you.

Upvotes: 1

Related Questions