簡體   English   中英

是否可以使用RxJava在非主線程中調用“onSensorChanged”方法?

[英]Is it possible to call “onSensorChanged” method in a non-main thread using RxJava?

我想使用RxJava在非主線程中調用“onSensorChanged”方法。

這是我的傳感器代碼。

    public class Accelerometer implements SensorEventListener {
    private Sensor linearAccelerSensor;

    static PublishSubject<SensorEvent> accelerData = PublishSubject.create();
    public static Observable<SensorEvent> getAccelerObservable(){ return accelerData; }

    public void initSensor(SensorManager sm){
        linearAccelerSensor = sm.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
        sm.registerListener(this,linearAccelerSensor,10000000);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        if(event.sensor == linearAccelerSensor){
            Log.d("Accelerometer","Acclerometer onSensorchanged");
            Log.d("Accelerometer",Thread.currentThread().getName())
            accelerData.onNext(event);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

這是我的服務onStartCommand代碼。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    myCompositeDisposable.add(Accelerometer.getAccelerObservable()
            .observeOn(Schedulers.computation())
            .subscribeOn(Schedulers.io())
            .subscribeWith(new DisposableObserver<SensorEvent>() {
                @Override
                public void onNext(SensorEvent sensorEvent) {

                    //textViewAccel.setText(sensorEvent.toString());
                    Log.d("ServiceAccelerometer",sensorEvent.toString());

                    lAccX = sensorEvent.values[0];
                    lAccY = sensorEvent.values[1];
                    lAccZ = sensorEvent.values[2];

                    lAccX = Math.round(lAccX*100)/100.0;
                    lAccY = Math.round(lAccY*100)/100.0;
                    lAccZ = Math.round(lAccZ*100)/100.0;

                    double accel = Math.sqrt((lAccX * lAccX) + (lAccY * lAccY) + (lAccZ * lAccZ));
                    accel = Math.round(accel*100)/100.0;
                    //textViewAccel.setText(lAccX+" "+lAccY+" "+lAccZ);
                    Log.d("ServiceAccelerometer",String.valueOf(accel));


                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            })

    );
    return START_NOT_STICKY;
}

實際上,我想在我的Service文件中訂閱onSensorChanged()方法。 但是,onSensorChange()方法只返回void,所以我需要創建另一個返回Observable()的方法。 所以我創建了getAccelerObservable(),它返回了Observable。 但在這個過程中,出現了問題。

onSensorChanged方法由主線程調用,因為我訂閱了getAccelerObservable()方法。 但是,我想在除mainThread之外的其他線程中調用onSensorChanged方法。 我該如何更正我的代碼?

OnSensorChanged不能在任何線程上調用,而是main。 RxJava甚至無法考慮它 - 框架調用它,框架不使用RxJava。 現在你可以做的是讓onSensorChange調用RxJava將數據從主線程推送到另一個線程。 這是一個好主意還是只會導致捶打取決於你對傳感器結果做了多少工作,以及之后是否需要更新UI。 這樣做可能會導致線程之間出現更多延遲和顛簸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM