我创建了两个LocationRequest
的实例,其间隔时间不同,我使用fusedLocation Api
进行位置更新。在位置更新中,如果用户没有移动,我将使用removeUpdates命令删除位置更新,并使用另一个位置请求实例请求新位置更新,并且如果用户正在移动,则类似地执行相同操作。但是一段时间后我每1秒获得一次位置更新(当用户没有移动时),如果用户正在移动,我每2秒获得一次位置更新。有时我也会得到更新。
我无法弄清楚为什么我得到了我从未设置的随机间隔时间段的位置更新。我假设它应该相应地为我提供位置更新设置的间隔时间。
任何人都可以帮我解决这个问题。提前谢谢:)
注意:没有其他请求应用的位置正在运行且fastestinterval
与setInterval
相同。
@Override
public void onConnected (@Nullable Bundle bundle){
//here mShortLocationRequest, mLongLocationRequest are instance of LocationRequest
//and SHORT_INTERVAL = 5000;
//LONG_INTERVAL = 20000
mShortLocationRequest = new LocationRequest();
mShortLocationRequest.setInterval(SHORT_INTERVAL);
mShortLocationRequest.setFastestInterval(SHORT_INTERVAL);
mShortLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLongLocationRequest = new LocationRequest();
mLongLocationRequest.setInterval(LONG_INTERVAL);
mLongLocationRequest.setFastestInterval(LONG_INTERVAL);
mLongLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mShortLocationRequest, this);
}
}
@Override
public void onLocationChanged (Location location){
//logic for isMoving here...
if (isMoving) {
//location updateds with long interval removed
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// here i am setting the short interval time by passing the mShortLocationRequest
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mShortLocationRequest, this);
isShortIntervalSet = true;
}
} else {
//location updateds with short interval removed
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// here i am setting the short interval time by passing the mShortLocationRequest
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLongLocationRequest, this);
isShortIntervalSet = false;
}
}
}