Is there a way to retrieve multiple sensor data in Android

后端 未结 1 1555
醉梦人生
醉梦人生 2020-12-02 17:55

I\'m trying to get both accelerometer and orientation data. Currently, I created an AccelerometerManager and OrientationManager. They both do the same thing; implement Senso

相关标签:
1条回答
  • 2020-12-02 18:38

    Can you try something like this:

    private SensorManager mSensorManager;
    private SensorEventListener mSensorListener;
    
    ////
    
    
    mSensorManager = (SensorManager) this
                    .getSystemService(Context.SENSOR_SERVICE);
    mSensorListener = 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) {
                //TODO: get values
            }else if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
                //TODO: get values
            }
        }
    }
    
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
    
    0 讨论(0)
提交回复
热议问题