Android:如何让 GPS 尝试寻找锁超时?

发布于 2025-01-08 19:59:52 字数 893 浏览 0 评论 0原文

以下是我的代码片段:

private class NoGpsLock implements Runnable {
    public void run() {
      if (gps == null) {
        Toast toast = Toast.makeText(getApplicationContext(), "Unable to find GPS lock", Toast.LENGTH_LONG);
        toast.show();
        if (locManager != null) {
          locManager.removeUpdates(locListener);
        }
      }
    }
  }

调用者

try {
  locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locListener);
} catch (Exception ex) {
}
Handler gpsHandler = new Handler();
gpsHandler.postDelayed(new NoGpsLock(), 15000); //15seconds

根据我的理解,removeUpdates() 应该停止 GPS 接收更新。我在模拟器和两台设备上尝试过这个;在模拟器上,它确实会阻止应用程序接收进一步的位置更改。然而,在实际设备上,“从 GPS 接收位置数据”的图标不断显示并耗尽电池(我认为这表明应用程序/手机一直在寻找位置但无法找到位置,因为我在室内) 。如何阻止 GPS 尝试查找位置?

编辑:我希望我的应用程序在一段时间后停止尝试查找位置,然后可能稍后再次重新启动 GPS。

Here are snippets of my code:

private class NoGpsLock implements Runnable {
    public void run() {
      if (gps == null) {
        Toast toast = Toast.makeText(getApplicationContext(), "Unable to find GPS lock", Toast.LENGTH_LONG);
        toast.show();
        if (locManager != null) {
          locManager.removeUpdates(locListener);
        }
      }
    }
  }

Called by

try {
  locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locListener);
} catch (Exception ex) {
}
Handler gpsHandler = new Handler();
gpsHandler.postDelayed(new NoGpsLock(), 15000); //15seconds

From what I understand, removeUpdates() is supposed to stop the GPS receiving updates. I tried this on the emulator and on 2 devices; on the emulator, it does stop the app from receiving further location changes. However, on the actual devices, the icon for "Receiving location data from GPS" keeps showing up and drains the battery (which I assume indicates that the app/phone keeps on looking for a location and unable to, since I'm indoors). How do I stop the GPS from trying to find a location?

EDIT: I want my app to stop trying to find location after a period of time, and then maybe restart the GPS again later.

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

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

发布评论

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

评论(1

百思不得你姐 2025-01-15 19:59:52

removeUpdates(locationListener)应该来停止闪烁的 GPS 图标。如果它没有这样做,我可以提供的最好建议是,也许还有其他侦听器实例仍在您的 locationManager 中注册?被遗忘的 for 循环可能附加了其他侦听器实例。作为一个暴力开发者,我会多次执行removeUpdates()来看看是否有任何效果。

removeUpdates(locationListener) is supposed to stop that blinking GPS icon. If it isn't doing that, the best suggestion I can offer is that maybe there are there are other listener instances still registered with your locationManager? A forgotten for-loop could have attached other listener instances. As a brute-force developer I'd do removeUpdates() multiple times to see if that has any effect.

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