Android:如何让 GPS 尝试寻找锁超时?
以下是我的代码片段:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.