简体   繁体   English

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

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

I want to call the "onSensorChanged" method in a non-main thread using RxJava. 我想使用RxJava在非主线程中调用“onSensorChanged”方法。

Here is my Sensor code. 这是我的传感器代码。

    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) {

    }
}

And This is my Service's onStartCommand code. 这是我的服务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;
}

Actually, I wanted to subscribe the onSensorChanged() method in my Service file. 实际上,我想在我的Service文件中订阅onSensorChanged()方法。 However, onSensorChange() method only returned void, so I need to make another method which returns the Observable(). 但是,onSensorChange()方法只返回void,所以我需要创建另一个返回Observable()的方法。 So I made the getAccelerObservable() which returns the Observable. 所以我创建了getAccelerObservable(),它返回了Observable。 But in this process, there was a problem. 但在这个过程中,出现了问题。

onSensorChanged method was called by main thread because I subscribed to the getAccelerObservable() method. onSensorChanged方法由主线程调用,因为我订阅了getAccelerObservable()方法。 However, I want to call onSensorChanged method in other thread except for mainThread. 但是,我想在除mainThread之外的其他线程中调用onSensorChanged方法。 How can I correct my code? 我该如何更正我的代码?

OnSensorChanged can't be called on any thread but main. OnSensorChanged不能在任何线程上调用,而是main。 RxJava can't even factor into it- the framework calls that, and the framework doesn't use RxJava. RxJava甚至无法考虑它 - 框架调用它,框架不使用RxJava。 Now what you can do is have onSensorChange call RxJava to push the data to another thread from the main thread. 现在你可以做的是让onSensorChange调用RxJava将数据从主线程推送到另一个线程。 Whether that's a good idea or will just lead to thrash depends on how much work you're doing with the sensor result, and whether you need to update the UI afterwards. 这是一个好主意还是只会导致捶打取决于你对传感器结果做了多少工作,以及之后是否需要更新UI。 Doing so may just cause more delay and thrashing between threads. 这样做可能会导致线程之间出现更多延迟和颠簸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM