将RxJava 1.0代码更新为RxJava 2.0

时间:2017-05-21 16:35:24

标签: android rx-android rx-java2

我正在更新一些旧的Android应用程序代码,无法弄清楚如何将在RxJava / RxAndroid 1.0中运行的代码转换为RxJava / RxAndroid 2.0。我试图转换的代码负责获取信号强度指标并每秒更新一次图表。我在RxJava 2.0中设法破解的东西根本不起作用,几秒钟之后应用程序崩溃了SIGABORT。我无法弄清楚我错过了什么?

我的RxJava 1代码:

rx.Observer myObserver = new rx.Observer<Float>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Float aFloat)
            {
                addEntry(aFloat);
            }
        };

        subscription = rx.Observable.interval(1, TimeUnit.SECONDS)
                            .flatMap(new Func1<Long, rx.Observable<Float>>() {
                                @Override
                                public rx.Observable<Float> call(Long aLong) {
                                    return getSignalStrength();
                                }
                            })
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(myObserver);


private rx.Observable<Float> getSignalStrength()
{
    return rx.Observable.create(subscriber ->
    {
        WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        Integer value = wifiManager.getConnectionInfo().getRssi();
        subscriber.onNext(value.floatValue());
        subscriber.onCompleted();
    });
}

我想要做RxJava 2.0:

        DisposableObserver<Float> disposableObserver = new DisposableObserver<Float>()
        {
            @Override
            public void onNext(Float value)
            {
                addEntry(value);
            }

            @Override
            public void onError(Throwable e)
            {
            }

            @Override
            public void onComplete()
            {
            }
        };

        Disposable disposable = Observable.interval(1, TimeUnit.SECONDS)
                .flatMap(new Function<Long, ObservableSource<Float>>()
                {
                    @Override
                    public ObservableSource<Float> apply(Long aLong) throws Exception
                    {
                        return signalStrengthInfo.getSignalStrength();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(disposableObserver);


private Observable<Float> getSignalStrength()
{
    return Observable.create(subscriber ->
    {
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        Integer signalStrength = wifiManager.getConnectionInfo().getRssi();
        subscriber.onNext(Float.valueOf(signalStrength));
        subscriber.onComplete();
    });
}

1 个答案:

答案 0 :(得分:1)

您在Rx2中的代码真的卷入其中。

只是看到简单的解决方案(抱歉kotlin),但是如果你在使用java时遇到问题,请在评论中ping我。

val disposable = Observable.interval(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap { getSignalStrength() }
                .doOnNext { addEntry(it) }
                .subscribe()


private fun getSignalStrength(): Observable <Float> {
            val wifiManager = this.getSystemService(Context.WIFI_SERVICE) as WifiManager
            val signalStrength = wifiManager.connectionInfo.rssi
            return Observable.just(signalStrength.toFloat())
    }