在 android 位置管理器中,我们应该提供更新的最小距离变化和更新之间的最短时间。因此,我需要知道我应该提供多长的最短距离范围和最短时间才能尽快知道当前位置?
1 回答
虽然将这两个值都设为 0 会给您带来最快的速度,但会消耗大量资源,因此会损失大量电池电量。如文档所述,要选择适当的值,您必须首先查看您的需求:
The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications.
此外,函数文档requestLocationUpdate
建议选择您的值,如下所示:
它说:
Choosing a sensible value for minTime is important to conserve battery life. Each location update requires power from GPS, WIFI, Cell and other radios. Select a minTime value as high as possible while still providing a reasonable user experience.
再具体一点:
If your application is not in the foreground and showing location to the user then your application should avoid using an active provider (such as NETWORK_PROVIDER or GPS_PROVIDER), but if you insist then select a minTime of
5 * 60 * 1000(5分钟) or greater. If your application is in the
前台 and showing location to the user then it is appropriate to select a
更新间隔更快。
至于 minDistance 参数:
If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed.
然而,位置提供者使用 minDistance 参数来节省电量更加困难,因此 minTime 应该是节省电池寿命的主要工具。
希望这可以帮助。