1

在我的應用程序中,我使用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... 
+1

有沒有機會註冊多個請求? – Blackbelt

+0

我,但是當我得到這樣的位置,我馬上註銷它:LocationRequest oneTimeOnStartRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) ... @覆蓋 公共無效呼叫(位置定位){ Log.d(標記,「獲取第一個位置更新...」); .. onlyFirstTimeSubscription.unsubscribe(); } }); –

+1

完成代碼請點擊這裏 – Blackbelt

回答

1

首先調整此:
你在MS setFastestInterval10 * 1000 - > 10分鐘,你需要做的:60 * 10 * 1000

再看看這個:
您需要創建一個您的位置提供程序類的實例:

如果您看these linksReaciveLocationProvider您會看到將當前上下文作爲參數傳遞。

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context); 
locationProvider.getLastKnownLocation() 
.subscribe(new Action1<Location>() { 
    @Override 
    public void call(Location location) { 
     doSthImportantWithObtainedLocation(location); 
    } 
}); 

reactiveLocationProvider = new ReactiveLocationProvider(this); 

作者傳遞上下文作爲參數。我建議你通過你所有的reactiveLocationProvider實例並創建一個實例。

因此,創建一個靜態的最終上下文,使用getapplicationcontext或類似的方法,如下所示。

例如:(這是從我的圖書館

LocationManager locationManager = (LocationManager) getSystemService(_CONTEXT); 

例採取在我的自定義類我通過最後的語境:

public class LocFinder extends Service implements LocationListener { 
    private final Context mContext; 

    public LocFinder(Context context) { 
     this.mContext = context; 
    } 

沒有更徹底的檢查GitHub的代碼很難說出作者是否對此有所保護,但是如果你是從MainActivity中調用它的話,那麼你可以創建一個新的請求。

request = LocationRequest.create() 

您還要求在onPause上的位置:

MainActivity.this.latitude = location.getLatitude(); 
MainActivity.this.longitude = location.getLongitude(); 

我不是100%,就需要做這一點。

創建locationrequest時,還要記下原作者使用final。這支持控制你的請求的想法調用應用程序的生命週期。

如果您需要更多幫助,請大聲說出。

+0

我沒有使用locationManager。它只是使用其方法來檢查位置服務是否已啓用。我爲我的位置更新/請求使用ReactiveLocationProvider庫 –

+0

@GeorgiKoemdzhiev我會看看,但它會與此有關。讓我看看需要做什麼。 brb –

+0

謝謝,非常感謝! :) –