我正在尝试来自http://developer.android.com/training/location/receive-location-updates.html的'LocationUpdates‘示例。此应用程序获取并打印位置通知。
我正在尝试根据我的最新位置更改位置更新的间隔。
因此,我将mLocationRequest.setInterval()添加到了onLocationChanged中
结果是非常错误的。我的应用程序被许多位置更新轰炸(每秒几次!)
我对示例的唯一更改是:
private int x=0;
@Override
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
mConnectionStatus.setText(R.string.location_updated);
// In the UI, set the latitude and longitude to the value received
mLatLng.setText(String.valueOf(x++));
mLocationRequest.setInterval(1000); // Change 1
mLocationClient.requestLocationUpdates(mLocationRequest, this); // Change 2
}
如何更改onLocationChanged内部的时间间隔?
我认为问题在于requestLocationUpdates重置了最后一个请求,然后立即发送另一个通知。这样就创建了一个循环。(比最快间隔更快)。所以我需要一种可靠的方法来改变‘活动’LocationRequest的时间间隔
发布于 2014-05-08 07:54:27
您不应该在onLocationChanged(Location location)
内部调用mLocationClient.requestLocationUpdates(mLocationRequest, this);
因为您正在重新注册侦听器,并且您将立即获得第一个呼叫。
所以我要做的是:
mLocationClient.requestLocationUpdates(mLocationRequest, this);
并查看mLocationRequest.setInterval(1000);
是否生效处理程序h=新处理程序();@Override public void onLocationChanged(位置位置){ //...你所有的代码运行( 1000);新的侦听器) h.postDelayed ()( mLocationClient.requestLocationUpdates(mLocationRequest,mLocationRequest.setInterval(){h.postDelayed void YOUROUTTERCLASS.this();} },1000);}
因此在一秒钟内没有注册的监听程序,因此您不会获得任何更新,在此之后,监听程序将按照该时间间隔进行注册。
发布于 2014-05-01 16:49:26
尝试使用mLocationRequest.setFastestInterval(long millis)
正如在developer.android.com中提到的:
这允许您的应用程序以比主动获取位置更快的速度被动获取位置,从而节省电力。与setInterval(long)不同,此参数是精确的。您的应用程序接收更新的速度永远不会超过此值。
发布于 2014-05-05 21:36:53
尝试使用:
mLocationRequest.requestLocationUpdates("gps", 1000, 0, this);
然而,我不同意在onLocationChanged事件中做requestLocationUpdates;在我看来,应该在onLocationChanged事件之外设置。
https://stackoverflow.com/questions/23324374
复制相似问题