我想觸發locationChanged方法來跟蹤實時位置。我使用下面的代碼來實現這一點。我的問題是我給了間隔5秒,但位置更改方法每3秒觸發一次。我嘗試改變優先順序,但它是一樣的。我理解其邏輯有什麼錯誤?預先感謝您的指導。LocationListener觸發位置請求之前設置的時間間隔使用gms android
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
locationRequest = new LocationRequest()
//tried high accuracy also thinking it might matter with priority but doesn't effect
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(5 * 1000);
觸發位置請求的方法。
private void requestLocation() {
if (Build.VERSION.SDK_INT >= 23) {
if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, locationListener);
else
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, locationListener);
Log.d(TAG, "requestLocation: ");
}
}
LocationListener的實例。
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//log shows this triggering at every 3 seconds.
Log.d(TAG, "onLocationChanged: ");
if (mGoogleApiClient.isConnected()) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
};
中描述的
setFastestInterval(long)
我可以通過將間隔時間設置爲10秒來實現5秒觸發,但是我想明白爲什麼這個時間差異存在? –爲什麼要投票? –