在RxJava 2中处理后,如何重新开始发射?

时间:2017-11-06 20:12:40

标签: android dispose rx-android rx-java2

我将我的observable放在onPause()中,我想在onResume()中重新启动它。我怎么能这样做?

这是我的观察结果:

    Observable<ObdCommandResult> myObservable = Observable.create(new ObservableOnSubscribe<ObdCommandResult>() {
        @Override
        public void subscribe(ObservableEmitter<ObdCommandResult> e) throws Exception {
            ...
            socket.connect();

            new ObdResetCommand().run(socket.getInputStream(), socket.getOutputStream());
            new EchoOffCommand().run(socket.getInputStream(), socket.getOutputStream());
            new LineFeedOffCommand().run(socket.getInputStream(), socket.getOutputStream());
            new SelectProtocolCommand(ObdProtocols.AUTO).run(socket.getInputStream(), socket.getOutputStream());
            ObdCommandResult obdCommandResult = new ObdCommandResult();
            while (!Thread.currentThread().isInterrupted()) {
                for (int i = 1; i < 5; i++) {
                    try {
                        livedataObdCommandList.get(i-1).getCommand().run(socket.getInputStream(), socket.getOutputStream());
                        obdCommandResult.setId(i);
                        obdCommandResult.setValue(livedataObdCommandList.get(i-1).getCommand().getFormattedResult());
                        e.onNext(obdCommandResult);
                    }catch (UnsupportedCommandException uce) {
                    } catch (InterruptedException ie) {
                }
            }
        }
    });

......和我的观察员:

    Observer<ObdCommandResult> observer = new Observer<ObdCommandResult>() {
        @Override
        public void onNext(ObdCommandResult value) {
            switch (value.getId())  {
                case 1:
                    currentSpeed.setText(value.getValue());
                    break;
                case 2:
                    revCounter.setText(value.getValue());
                    break;
                case 3:
                    throttlePosition.setText(value.getValue());
                    break;
                case 4:
                    oilTemp.setText(value.getValue());
                    break;
                case 5:
                    fuelLevel.setText(value.getValue());
                    break;
                case 6:
                    engineCoolant.setText(value.getValue());
                    break;
            }
        }

    };

所以我想处理它,并在用户返回时重新启动。

0 个答案:

没有答案