Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestLocationUpdates interval in Android

I try to get the correct speed in updates for the function onLocationChanged, this is my class:

public class LocationService extends Service implements LocationListener {

Putting the minTime on 6000 does not help, it wil keep updating constantly, what am i doing wrong?

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper) {

Greetings

like image 985
Webbo Avatar asked Oct 22 '10 03:10

Webbo


2 Answers

The minTime is just a hint for the LocationProvider, and it doesn't mean that your location listener will be called once every 6 seconds. You will receive more location updates, and its up to your code to pick the most accurate one.

Monitor the GPS icon on your phone. A call to requestLocationUpdates will trigger the GPS to pinpoint your location, and it will send one or more location updates to the locationlistener if it's able to get a fix. (At this point, your GPS icon should be animated as it searches for a location).

During that time, your locationlistener may receive several location updates. Your code can go and pick the most accurate location, and process only that one.

After the GPS has sent the location update(s) to your listener, there should be a period of inactivity. (your GPS icon should disappear for a couple of seconds). This period of inactivity should correspond with your minTime. The status of the GPS will also change, as it will be put into TEMPORARILY_UNAVAILABLE.

After that, the same process is repeated. (The GPS becomes AVAILABLE, and you'll again receive one or more location updates).

Also take into account, if the GPS is unable to get a location fix, the GPS icon will remain active for more then 6 seconds, but you won't be receiving location updates.

You can also monitor the status of your GPS provider through your listener, via the following method :

public void onStatusChanged(String provider, int status, Bundle extras) {}

The status is one of the following constants defined on android.location.LocationProvider

public static final int OUT_OF_SERVICE = 0;
public static final int TEMPORARILY_UNAVAILABLE = 1;
public static final int AVAILABLE = 2;

Have a look at Understanding the LocationListener in Android for an example on the minTime behavior, and a scenario (including some logging) to help you understand what's going on.

Keep in mind that tweaking the minTime and minDistance parameters on the LocationManager, and acting upon GPS status updates will allow you to fine-tune your user location development.

like image 130
ddewaele Avatar answered Nov 15 '22 10:11

ddewaele


6000 in milliseconds equals 6 seconds, and it may seems like continiously updating. From Android dev guide "minTime under 60000ms are not recommended" Maybe it is worth to increase it to 60000ms

like image 31
6istik Avatar answered Nov 15 '22 09:11

6istik