requestLocationUpdates minTime parameter purpose

拈花ヽ惹草 提交于 2019-12-23 13:09:17

问题


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:

  1. 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?
  2. 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?

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13555153/requestlocationupdates-mintime-parameter-purpose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!