Android 位置监听器在我告诉的时间内没有更新
我希望 Android 上的 LocationListener 要求每 5 秒更新一次
我这样做:
locMgr.requestLocationUpdates(high.getName(), (long) 5000, 0,
UpdL1 = new LocationListener() {
public void onLocationChanged(Location location) {
// do something here to save this new location
if (location != null) {
//
updateARLoc(location.getLatitude(),location.getLongitude());
//
if(!creada){
locMgr.removeUpdates(UpdL1);
}
} else {
}
}
我告诉它每 5000 毫秒更新一次,即 5 秒。但它一直在不断更新。 logcat 告诉我每 500 毫秒更新一次。 (打印的每个“A”都是来自侦听器的更新)
09-14 14:57:29.998: INFO/info(3456): A
09-14 14:57:30.018: INFO/info(3456): A
09-14 14:57:30.918: INFO/info(3456): A
09-14 14:57:30.928: INFO/info(3456): A
09-14 14:57:31.988: INFO/info(3456): A
09-14 14:57:31.998: INFO/info(3456): A
09-14 14:57:32.958: INFO/info(3456): A
09-14 14:57:32.988: INFO/info(3456): A
09-14 14:57:33.948: INFO/info(3456): A
09-14 14:57:33.948: INFO/info(3456): A
I want the LocationListener on Android to ask for update every 5 seconds
I am doing this:
locMgr.requestLocationUpdates(high.getName(), (long) 5000, 0,
UpdL1 = new LocationListener() {
public void onLocationChanged(Location location) {
// do something here to save this new location
if (location != null) {
//
updateARLoc(location.getLatitude(),location.getLongitude());
//
if(!creada){
locMgr.removeUpdates(UpdL1);
}
} else {
}
}
I am telling it to update every 5000 miliseconds, that's 5 seconds. But it keeps updating all the time. The logcat is telling me that is updating like every 500 miliseconds. (Every "A" printed is an update from the listener)
09-14 14:57:29.998: INFO/info(3456): A
09-14 14:57:30.018: INFO/info(3456): A
09-14 14:57:30.918: INFO/info(3456): A
09-14 14:57:30.928: INFO/info(3456): A
09-14 14:57:31.988: INFO/info(3456): A
09-14 14:57:31.998: INFO/info(3456): A
09-14 14:57:32.958: INFO/info(3456): A
09-14 14:57:32.988: INFO/info(3456): A
09-14 14:57:33.948: INFO/info(3456): A
09-14 14:57:33.948: INFO/info(3456): A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://developer.android.com/reference /android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)
在 requestLocationUpdates 方法的参数下方,它指出:
“通知,以毫秒为单位。此字段仅用作节省电量的提示,位置更新之间的实际时间可能大于或小于此值。”
requestLocationUpdates() 使机器人的 GPS 尝试修复您的位置。尝试修复时,您的位置侦听器可能会收到大量位置更新。
在收到位置更新后,GPS 应关闭您在 mintime 参数中指定的时间。
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)
Right under parameters for the requestLocationUpdates method it states:
"the minimum time interval for notifications, in milliseconds. 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."
requestLocationUpdates() makes the droid's GPS try to get a fix on your location. While trying to get the fix, your locationlistener could get tons of location updates.
After receiving the location updates, then the GPS should turn off for the amount of time you specified in mintime parameter.
正如您在 android 文档中可以找到的:
通知的频率可以使用 minTime 和 minDistance 参数来控制。如果 minTime 大于 0,LocationManager 可能会在位置更新之间休息 minTime 毫秒以节省电量。如果 minDistance 大于 0,则仅当设备移动了 minDistance 米时才会广播位置。要尽可能频繁地获取通知,请将两个参数都设置为 0。
换句话说,如果您的设备不需要节省电量,它将在需要时发送更新。
此外,您已将 minDistance 参数设置为 0,这意味着您希望每 0 米更新一次(尽可能频繁)。
As you can find in android documentation:
The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.
In other words, if your device do not need to save power it will send update when it wants.
Furthermore, you've set the minDistance parameter to 0 which means you want an update every 0 meters (so as often as possible).