API sensore dell'angolo della cernieraHinge Angle Sensor API

EsempioExample

Esposto tramite il sensore dell'angolo della cerniera e tramite SensorEventListener().Exposed via Hinge Angle sensor and using SensorEventListener().

private static final String HINGE_ANGLE_SENSOR_NAME = "Hinge Angle";

private SensorManager mSensorManager;
private Sensor mHingeAngleSensor;
private SensorEventListener mSensorListener;


private void setupSensors() {
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);

    for (Sensor sensor : sensorList) {
        if (sensor.getName().contains(HINGE_ANGLE_SENSOR_NAME)) {
            mHingeAngleSensor = sensor;
        }
    }

    mSensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(final SensorEvent event) {
            if (event.sensor == mHingeAngleSensor) {
                int angle = (int) event.values[0];
            }
        }

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

@Override
protected void onPause() {
    super.onPause();
    if(mHingeAngleSensor !=null) {
        mSensorManager.unregisterListener(mSensorListener,mHingeAngleSensor);
    }
}

@Override
protected void onResume() {
    super.onResume();
    if(mHingeAngleSensor !=null) {
        mSensorManager.registerListener(mSensorListener, mHingeAngleSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
}