开发者

Why is my apps location being updated twice as fast as I've specified?

In my application, I've setup a location manager to update the current location every 10 minutes (or so I thought). Instead I'm getting updates every 5 minutes:

10-03 23:45:17.153: DEBUG/TheApp(2025): Location updated with accuracy: 48.0m
10-03 23:50:23.162: DEBUG/TheApp(2025): Location updated with accuracy: 48.0m
10-03 23:55:23.074: DEBUG/TheApp(2025): Location updated with accuracy: 48.0m
10-04 00:00:23.077: DEBUG/TheApp(2025): Location updated with accuracy: 48.0m

Here's the code:

LocationManager locationManager = (LocationManager)
                   this.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        Log.d(APP_TAG, "Location updated with accuracy: " + location.getAccuracy() + "m");
    }
    //Other methods are empty and omitted for brevity
};

int TEN_MINUTES = 10 /*Minutes*/ * 60 /*sec per min*/ * 1000 /*ms per sec*/;
locationManager.requestLocationUpdates(LocationManager.N开发者_高级运维ETWORK_PROVIDER, 
                                         TEN_MINUTES, 0, locationListener);

Can anyone explain why this isn't working as I thought? E.g. why its updating every 5 minutes instead of 10?


As noted in the documentation for LocationManager.requestLocationUpdates():

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.

So, there's no guarantee that you won't get updates more (or less) frequently. It's possible that the GPS chip is able to provide more accurate location information without consuming additional power or otherwise doesn't allow the requested level of granularity. It's also possible that another application is also requesting updates in the background.

Answering with more detail that that would probably require knowledge of how the specific GPS receiver in your phone works.


Are you sure its not just your log updating every five minutes?

When I did something like this under the locationListener I put a check to make sure the 10 minutes had actually passed.

if(System.CurrentTimeMillis() > updateTime + currentTimeInMillis)
{
 update your log here
 currentTimeInMillis = System.CurrentTimeMillis();
}


The behaviour is quirky to say the least. The documentation says that these parameters are only hints. One question which I can't really answer is:

Are the time and the distance arguments logical ORed or logical ANDed in deciding whether to send an update?

If it's an OR condition, then if the OS thinks you've moved more than the min distance it might update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜