Get multiple sensor data at the same time in Android

前端 未结 1 994
南笙
南笙 2020-12-20 16:16

I am now trying to create an app to monitoring the vibration. I use accelerometer to finish the job, when the recorded acceleration exceed certain threshold, I call it a tri

相关标签:
1条回答
  • 2020-12-20 16:46

    Yes you can do this as follows:

    private SensorManager manager;
    private SensorEventListener listener;
    
    manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
    listener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            Sensor sensor = event.sensor;
            if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                ...
            }
            else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                ...
            }
        }
    }
    
    manager.registerListener(listener, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
    manager.registerListener(listener, manager.getDefaultSensor(TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
    
    0 讨论(0)
提交回复
热议问题