Android Locationprovider 需要太长时间才能暂时不可用

发布于 2024-10-20 14:19:39 字数 1574 浏览 5 评论 0原文

根据这篇文章(UPD:链接已删除,因为它现在会导致一些废话),在请求位置更新时设置 minTime 将导致提供程序将自身设置为 TEMPORARILY_UNAVAILABLE < em>minTime 毫秒,以节省电池电量。在此不活动期间,GPS 提供商将自行关闭,并且 GPS 图标将消失。

在我的代码中,我将 minTime 设置为大约 30 秒,但提供程序仅每五分钟变为 TEMPORARILY_UNAVAILABLE 一次。当它发生时,它最多只会保持 TEMPORARILY_UNAVAILABLE 十秒钟,然后再重新打开。我之所以知道这一点,是因为 GPS 图标只消失了十秒钟,然后又重新出现。

据我所知,minTime 设置只是 Android 位置提供程序的粗略指导...但我很确定 5 分钟与 30 秒完全不同。有谁知道这是怎么回事? minTime 和 requestLocationUpdates 实际上是如何工作的?

位置管理器设置:

locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,30000L, 0, locListener);

locListener:

public void onLocationChanged(Location loc) {
    //Keep track of best location
    //Having a location > no location
    if (bestLocation == null)
        bestLocation = loc;
    //More accuracy > Less accuracy
    else if (loc.getAccuracy() <= bestLocation.getAccuracy())
        bestLocation = loc;

    Log.d(TAG, "Location Updated";
}
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d(TAG, "New status: " + status);
    if (status== LocationProvider.TEMPORARILY_UNAVAILABLE)
        //Do stuff since the provider is temporarily off
}

在真实 Android 设备上调试输出 (HTC Incredible 2.2):

Location Updated
Location Updated
New status: 2
Location Updated
Location Updated
Location Updated
New status: 2
... (five minutes later)
New status: 1

According to this article (UPD: link removed as it leads to some crap now), setting the minTime when requesting location updates will cause the provider to set itself to TEMPORARILY_UNAVAILABLE for minTime milliseconds in order to conserve battery power. During this period of inactivity, the GPS provider will turn itself off and the GPS icon will disappear.

In my code, I set the minTime to about 30 seconds, but the provider only becomes TEMPORARILY_UNAVAILABLE once every five minutes. When it does, it only stays TEMPORARILY_UNAVAILABLE for ten seconds at most before turning itself back on. I know this because the GPS icon disappears for only ten seconds before reappearing again.

I understand that the minTime setting is only a rough guideline for the Android location provider...but I'm pretty sure that five minutes is entirely different from 30 seconds. Does anyone know what is going on here? How does minTime and requestLocationUpdates actually work?

LocationManager Setup:

locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,30000L, 0, locListener);

locListener:

public void onLocationChanged(Location loc) {
    //Keep track of best location
    //Having a location > no location
    if (bestLocation == null)
        bestLocation = loc;
    //More accuracy > Less accuracy
    else if (loc.getAccuracy() <= bestLocation.getAccuracy())
        bestLocation = loc;

    Log.d(TAG, "Location Updated";
}
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d(TAG, "New status: " + status);
    if (status== LocationProvider.TEMPORARILY_UNAVAILABLE)
        //Do stuff since the provider is temporarily off
}

Debug output on a real Android device (HTC Incredible 2.2):

Location Updated
Location Updated
New status: 2
Location Updated
Location Updated
Location Updated
New status: 2
... (five minutes later)
New status: 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-10-27 14:19:39

来自 API

“后台服务应谨慎设置足够高的 minTime,以便设备不会因始终保持 GPS 或无线电而消耗太多电量。特别是, 不建议使用低于 60000ms 的值。”(重点是我的)

“此字段仅用作节省电量的提示,位置更新之间的实际时间可能会更长或更长小于这个值。” (重点是我的)。

如果您需要立即知道位置,请使用 getLastKnownLocation(String)

From the API:

"Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended." (emphasis is mine)

"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." (emphasis is mine).

If you need to know the location immediately, use getLastKnownLocation(String)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文