在后台进行过于频繁的位置更新

时间:2015-12-28 15:26:42

标签: android background location location-provider fusedlocationproviderapi

在我的应用中,我使用ReactiveLocationProvider来获取后台位置更新。 (库link)当我的应用程序最小化(进入onPause)时,它开始接收更新。它工作正常,但问题是我不想经常更新。例如,在我的代码中,当用户移动1km并且至少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 个答案:

答案 0 :(得分:1)

首先调整一下:
您的setFastestInterval毫秒10 * 1000 - &gt; 10分钟你需要做到这一点:60 * 10 * 1000

然后看看这个:
您需要在应用期间创建一个位置提供者类的实例:

如果您查看these links ReaciveLocationProvider,您会看到将当前上下文作为参数传递。

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代码,很难说作者是否已经安全地防范了这一点,但是如果你在onAate中从MainActivity中调用它,那么你可能会创建一个新的请求。

  request = LocationRequest.create()

您还要求OnPause上的位置:

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

我并非100%明白是否需要这样做。

还要记下原创作者在创建位置请求时对final的使用。它支持在应用程序生命周期内控制请求调用的想法。

如果您需要更多帮助,请大声说出来。