在我的應用程序中,我使用ReactiveLocationProvider來獲取後臺位置更新。 (庫link)當我的應用程序最小化(進入onPause)時,它開始接收更新。它工作正常,但問題是我不希望頻繁的更新。例如,在我的代碼中,當用戶移動1公里並且至少10分鐘(setFastestInterval)已通過時,我有一個LocationRequest(假設爲)以獲取位置更新(一)。根據我的日誌,我收到了比需要更多的更新。有人知道爲什麼在後臺獲取過於頻繁的位置更新
這是我的代碼在我的創建方法MainActivity:
public class MainActivity extends AppCompatActivity {
LocationRequest request;
ReactiveLocationProvider locationProvider;
Subscription subscription;
Subscription onlyFirstTimeSubscription;
NotAbleToGetWeatherDataTask mNotAbleToGetWeatherDataTask = new NotAbleToGetWeatherDataTask();
int numOfBackgroundUpdates = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----------MY CODE STARTS HERE-----------------
request = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setSmallestDisplacement(1000)
.setFastestInterval(10 * 1000)
.setInterval(30 * 60 * 1000);
locationProvider = new ReactiveLocationProvider(this);
}
和我的onPause方法:
@Override
protected void onPause() {
super.onPause();
//subscribe for background location updates...
subscription = locationProvider.getUpdatedLocation(request)
.subscribe(new Action1<Location>() {
@Override
public void call(Location location) {
Log.d(TAG, "Getting Background updates...");
MainActivity.this.latitude = location.getLatitude();
MainActivity.this.longitude = location.getLongitude();
numOfBackgroundUpdates++;
}
});
}
,我從請求上摧毀退訂:
@Override
protected void onDestroy() {
Log.d(TAG, "OnDestroy Called!");
subscription.unsubscribe();
super.onDestroy();
}
最近的日誌(我已經移動了1公里,至少10分鐘已經過去了)
12-28 17:12:25.564 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.918 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.924 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.925 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.927 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.928 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.930 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.931 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.940 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.942 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.942 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.946 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.949 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.951 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.951 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:06.951 29845-29845/? D/MainActivity: Getting Background updates...
12-28 17:16:40.371 29845-29845/? D/MainActivity: Getting Background updates...
有沒有機會註冊多個請求? – Blackbelt
我,但是當我得到這樣的位置,我馬上註銷它:LocationRequest oneTimeOnStartRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) ... @覆蓋 公共無效呼叫(位置定位){ Log.d(標記,「獲取第一個位置更新...」); .. onlyFirstTimeSubscription.unsubscribe(); } }); –
完成代碼請點擊這裏 – Blackbelt