我在onPause()中处理我的Observable,我希望在onResume()中重新启动它。我该怎么做?
以下是我的Observable:
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;
}
}
};
因此,我希望将其处理掉,并在用户返回时重新启动。
Observer<ObdCommandResult> observer = new Observer<ObdCommandResult>() { @Override public void onNext(ObdCommandResult value) { ..
) - c0dehunter