FusedLocationProviderClient rxjava2包装器

时间:2018-10-18 14:49:31

标签: android location rx-java2

我正在尝试将LocationCallback包裹在一个可观察到的地方,我试图那样做

 private Observable<Location> createLocationObservable(){

    return Observable.create(new ObservableOnSubscribe<Location>() {
        @Override
        public void subscribe(final ObservableEmitter<Location> emitter) throws Exception {
             mLocationCallback=new LocationCallback(){
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    if (locationResult == null) {
                        return;
                    }
                    emitter.onNext(locationResult.getLastLocation());
                }
            };
             startLocationUpdates();
        }
    });
}

private void startLocationUpdates() {

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermission();
        return;
    }
    getFusedLocationProviderClient(context).requestLocationUpdates(mLocationRequest,
            mLocationCallback,
            null /* Looper */);
}

我在这个可观察的物体上进行观察,但是观察者的onNext方法未接收到任何位置,那么怎么了?

0 个答案:

没有答案