Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: requestLocationUpdates updates location at most every 45 seconds
    primarykey
    data
    text
    <h2>Background</h2> <p>I am writing an Android app whose main function is tracking the user's location and making an alert when the user gets near some point. Therefore I need to update the user's location at regular intervals, and these intervals should get smaller as the user comes closer to the target. So when the user is within, say, 1 km of the target, I want the location to be updated every 20 seconds and so on, until the user arrives.</p> <h2>Problem</h2> <p>When I test it (<code>provider = LocationManager.NETWORK_PROVIDER</code>), a call to <code>requestLocationUpdates(provider, minTime, minDistance, locationListener)</code> with any <code>minTime &lt; 45000</code> has the same effect as <code>minTime = 45000</code>, i.e. I get updates with an interval of <em>exactly</em> 45 seconds.<br> I know the minimum time parameter is only a "hint", but it is not taken as a hint by my app. I get updates with the interval specified until that interval passes below 45 seconds. It seems as though a minimum time of 45 seconds between location updates is hardcoded into Android, but that would be kind of odd. Plus I have never heard of this problem before, and I have not been able to find it addressed here on Stackoverflow.</p> <p>Because I am not able to get frequent updates, my workaround (for now) is to manually call <code>requestLocationUpdates</code> whenever a new location is needed, and then just use the first available location. To do this at small intervals I use <code>handler.postDelayed(myRunnable, updateInterval)</code> to delay the calls, and <code>myRunnable</code> then takes care of calling <code>requestLocationUpdates</code>. However, this method only works about 50 (apparently random) percent of the time.</p> <p>Does anybody know of the problem, and is there a way to fix it? Or is my only option to set <code>minTime = 0</code> and just hope for the best?</p> <h2>Source code</h2> <p>Here is the source code for myRunnable, whose <code>run()</code> method I manually call regularly with <code>handler.postDelayed(myRunnable, updateInterval)</code>:</p> <pre><code>public class MyRunnable implements Runnable { private LocationManager manager; private LocationListener listener; @Override public void run() { // This is called everytime a new update is requested // so that only one request is running at a time. removeUpdates(); manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); listener = new LocationListener() { @Override public void onLocationChanged(Location loc) { location = loc; latitude = loc.getLatitude(); longitude = loc.getLongitude(); accuracy = Math.round(loc.getAccuracy()); handler.sendMessage(Message.obtain(handler, KEY_MESSAGE_LOCATION_CHANGED)); checkForArrival(); } // Other overrides are empty. }; if(!arrived) manager.requestLocationUpdates(provider, updateInterval, 0, listener); } /** * Removes location updates from the LocationListener. */ public void removeUpdates() { if(!(manager == null || listener == null)) manager.removeUpdates(listener); } // Another method for "cleaning up" when the user has arrived. } </code></pre> <p><br> And here is my <code>handler</code>:</p> <pre><code>handler = new Handler() { @Override public void handleMessage(Message msg) { switch(msg.what) { case KEY_MESSAGE_LOCATION_CHANGED: if(myRunnable != null) { myRunnable.removeUpdates(); handler.postDelayed(myRunnable, updateInterval); } break; } } }; </code></pre> <p><br></p> <h2>Additional info</h2> <p>The whole location updating thing runs in a service.</p> <p>I have read the doc several times, Google'd the problem, and tried various other workarounds. Nothing quite does it.</p> <p>I have logged the damn out of this thing, and the only exciting thing to see is a big fat "ignore" to my frequent location requests. All the right methods are called.</p> <p>Any help will be <em>very much</em> appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload