我正在尝试使用 Rx-Java 创建一个用于 Android 上位置跟踪的类。我仍然不知道如何正确处理我的 Observable 的生命周期。我想要的是一个 Observable,它在第一次订阅发生时开始跟踪位置,并在最后一次订阅被丢弃时停止位置跟踪。到目前为止我所取得的成就是:
public class LocationObservable
implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
private LocationClient locationClient;
private final PublishSubject<Location> latestLocation =
PublishSubject.create();
public final Observable<Location> locationObservable =
Observable.defer(() -> {
if(!locationClient.isConnected() && !locationClient.isConnecting()) {
locationClient.connect();
}
return latestLocation.asObservable().scan((prev, curr) -> {
if (Math.abs(prev.getLatitude() - curr.getLatitude()) > 0.000001 ||
Math.abs(prev.getLongitude() - curr.getLongitude()) > 0.000001)
return curr;
else
return prev;
}).distinctUntilChanged();});
public LocationObservable(Context context) {
locationClient = new LocationClient(context, this, this);
}
@Override
public void onConnected(Bundle bundle) {
latestLocation.onNext(locationClient.getLastLocation());
}
@Override
public void onDisconnected() {
latestLocation.onCompleted();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
latestLocation.onError(new Exception(connectionResult.toString()));
}
@Override
public void onLocationChanged(Location location) {
latestLocation.onNext(location);
}
}
正如你所看到的,我使用Observable#defer
当第一个客户端订阅时初始化位置回调。我不知道这是否是一个好方法,但这是我目前想到的最好的方法。我仍然缺少的是当我的班级的最后一个客户取消订阅我的可观察对象时如何停止位置更新。或者也许这是 Rx 中不惯用的东西,因为它并不明显?
我相信,这个用例应该相当标准,因此应该有一个标准/惯用的解决方案。很高兴知道这一点。
private LocationClient locationClient;
private final Observable<Integer> locationObservable = Observable
.create(new OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
locationClient.connect();
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
if (locationClient.isConnected()
|| locationClient.isConnecting()) {
locationClient.disconnect();
}
}
}));
}
}).multicast(PublishSubject.<Integer> create()).refCount();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)