在 onSensorChanged 上读取数据时出现问题

Problems reading data on onSensorChanged

onSensorChanged 上有 data 在传感器打开时连续 出现。

我想:

  1. 开启传感器10秒,每隔10秒读取一次数据。
  2. 关闭传感器 20 秒。
  3. 不断重复同样的事情(步骤 1 到 2)。

我使用 ScheduledExecutorService.scheduleAtFixedRate 定期从 onSensorChanged 读取数据,但它不起作用。有什么解决办法?

public class ProximityService extends Service {

    @Override
    public void onCreate() {
        HandlerThread t = new HandlerThread("ProximityServiceHandler", THREAD_PRIORITY_BACKGROUND);
        t.start();
        _serviceLooper = t.getLooper();
        _serviceHandler = new ProximityServiceHandler(_serviceLooper, this);
    }


    private final class ProximityServiceHandler extends Handler {
        private static final int ON_TIME = 10000;   // 10 seconds
        private static final int OFF_TIME = 20000;  // 20 seconds

        private void parseSensor(SensorEvent sensorEvent) {
            // data processing
        }

        @Override
        public void handleMessage(Message msg) {
            _sensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE));
            if (_sensorManager != null) {

                _sensor = _sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
                if (_sensor != null) {

                    _sensorEventListener = new SensorEventListener() {
                        @Override
                        public void onSensorChanged(SensorEvent sensorEvent) {

                            if (sensorEvent != null) {
                                // collect data..
                                // data available in here, but not in _sensorScheduler?
                                parseSensor(sensorEvent); 
                            }
                        }

                        @Override
                        public void onAccuracyChanged(Sensor sensor, int i) {       
                            //
                        }
                    };

                    // why this function doesn't worked ?
                    _sensorScheduler = Executors.newScheduledThreadPool(1);
                    _sensorScheduler.scheduleAtFixedRate(
                            new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        // turnon sensor
                                        _sensorManager.registerListener(_sensorEventListener, _sensor, SensorManager.SENSOR_DELAY_NORMAL);

                                        // wait to collect data from sensor
                                        Thread.sleep(ON_TIME);

                                        // send data to main thread but data not available in here?
                                         sendData(sensorEvent)

                                        // turn off sensor
                                        _sensorManager.unregisterListener(_sensorEventListener, _sensor);

                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, 0, OFF_TIME, TimeUnit.MILLISECONDS);
                }
            }
        }

    }
}

我找到了解决方案,在 sendData(sensorEvent) 中调用 toast,这使传感器停止工作。

我修改了 toast 以便能够从后台线程 调用,如下所示:

public static void showToast(final Context context, final String text, final int duration) {
    if (context != null && text != null) {
        new Handler(context.getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(context, text, duration).show();
            }               
        });
    }
}