LocationRequest setInterval在android中工作不正常
我已经创建了两个间隔时间不同的LocationRequest setInterval在android中工作不正常,android,location,google-api-client,Android,Location,Google Api Client,我已经创建了两个间隔时间不同的LocationRequest实例,我正在使用fusedLocation Api进行位置更新。在位置更新中,如果用户不移动,我将使用removeUpdates命令删除位置更新,并使用位置请求的另一个实例请求新的位置更新,同样,如果用户正在移动,我也会这样做。但过了一段时间后,我会每1秒(当用户不移动时)得到一次位置更新,如果用户移动,我会每2秒得到一次位置更新。而且有时更新太迟了 我不明白为什么我会得到我从未设置过的随机间隔时间段的位置更新。我假设它应该根据设置的间
LocationRequest
实例,我正在使用fusedLocation Api
进行位置更新。在位置更新中,如果用户不移动,我将使用removeUpdates命令删除位置更新,并使用位置请求的另一个实例请求新的位置更新,同样,如果用户正在移动,我也会这样做。但过了一段时间后,我会每1秒(当用户不移动时)得到一次位置更新,如果用户移动,我会每2秒得到一次位置更新。而且有时更新太迟了
我不明白为什么我会得到我从未设置过的随机间隔时间段的位置更新。我假设它应该根据设置的间隔时间为我提供相应的位置更新
有谁能帮我解决这个问题吗。thanx提前:)
注意:没有其他位置请求应用正在运行,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;
}
}
}
请你试着解释清楚你的问题。这句话“如果用户不移动,我将删除位置更新,并使用另一个位置请求实例请求新位置更新,同样,如果用户正在移动,他也会这样做。”正在造成混乱。正如您在我的
onLocationChanged
代码中看到的,为了更新LocationRequest的间隔时间,我使用了两个LocationRequest实例。所以在onLocationChanged中,我检查用户是否在同一位置。如果用户在同一位置,我将使用LocationServices.FusedLocationApi.RemovelocationUpdate(mgoogleapClient,this)删除旧的locationRequest(即shortLocation)代码>&使用LocationServices.FusedLocationApi.RequestLocationUpdate请求新位置更新(mgoogleapClient、mLongLocationRequest、this)代码>通过将新的mLongLocationRequest传递给fusedApii,我不知道您是否得到它。事实上,我想知道为什么我会得到随机时间间隔的位置更新。当我在代码中将间隔时间明确设置为5秒和20秒时。