1

在我的应用程序中,我使用 ReactiveLocationProvider 来获取后台位置更新。(库链接)当我的应用程序最小化(进入 onPause)时,它开始接收更新。它工作正常,但问题是我不希望频繁更新。例如,在我的代码中,我有一个 LocationRequest,它应该在用户移动 1 公里并且至少 10 分钟(setFastestInterval)已经过去时获取位置更新(一个)。根据我的日志,我收到的更新超出了需要。有谁知道为什么?

这是我在 MainActivity on create 方法中的代码:

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...
4

1 回答 1

1

首先调整这个:
setFastestInterval的毫秒是10 * 1000-> 10分钟你需要这样做:60 * 10 * 1000

然后看看这个:
您需要在应用程序的持续时间内创建一个位置提供程序类的实例:

如果您查看这些指向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 代码,很难说作者是否对此进行了安全防范,但如果您像这样在 oncreate 中从 MainActivity 调用它,那么您可能正在创建一个新请求。

  request = LocationRequest.create()

您还要求 OnPause 上的位置:

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

我不是 100% 清楚是否需要这样做。

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

如果您需要更多帮助,请大声喊叫。

于 2015-12-28T15:47:16.570 回答