我有两个方法在我的应用程序,以获得用户的位置。这两种方法都是可观察的。第一种方法是通过位置管理器获得gps定位。第二种方法是融合定位。
@SuppressLint("MissingPermission")
public Observable<Location> getRxGpsLocation(boolean single)
{
PublishSubject<Location> subject = PublishSubject.create();
LocationManager locationManager = (LocationManager) activity.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
5, new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
subject.onNext(location);
if (single)
{
locationManager.removeUpdates(this);
subject.onComplete();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: Disableeedd");
if (single)
{
locationManager.removeUpdates(this);
}
subject.onError(new Throwable("Error nit found Location"));
}
});
return subject;
}
//I use this library to get Fused Location https://github.com/patloew/RxLocation
@SuppressLint("MissingPermission")
public Observable<Location> getRxFusedLocation()
{
RxLocation rxLocation = new RxLocation(AppClass.getApp());
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setNumUpdates(1)
.setInterval(1000);
return rxLocation.location().updates(locationRequest);
}所以我的目标是提出这一连串的请求:
在第一次请求之后,我如何将这两种方法合并成一个15s定时器?
发布于 2020-02-24 15:17:48
简而言之,为了你的问题。您必须从LocationManager创建流,从FusedLocationProviderClient创建流,然后应用“超时值”操作。例如:
locationManagerObservable()
.timeout(15, TimeUnit.SECONDS)
.onErrorResumeNext { error ->
if(error is TimeoutException)
fusedLocationObservable()
else
Observable.error(error)
}用一个主题来达到你的目的并不是一个好的选择。您应该研究如何从回调API中创建可观察的API。
https://stackoverflow.com/questions/56990968
复制相似问题