Detección de la orientación del teléfono con eventos del sensor

Tengo una actividad, que necesita su orientación para ser bloqueado con

setRequestedOrientation(screenOrientation); 

Pero quiero obtener actualizaciones de orientación, por lo que puedo hacer ajustes a la interfaz de usuario (imaginar la aplicación de la cámara HTC, cuando sólo los iconos del botón cambian de orientación). Así que encontré esta clase . Proporciona valores de orientación, entre 0 y 360. ¿Cómo puedo filtrar estos valores, es decir, el intervalo perfecto [a, b] , y si a<x<b entonces la orientación es horizontal o vertical? Calcular medio? ¿Algún consejo?

Parece que necesita código para reaccionar sólo cuando la orientación del dispositivo ha cambiado a una de las 4 orientaciones normales en lugar de a cada ángulo. Esto filtra la orientación a sólo valores de 0, 90, 180 y 270 grados:

 OrientationEventListener myOrientationEventListener; int iOrientation; myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int iAngle) { // 0 15 30 45 60 75, 90 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345 final int iLookup[] = {0, 0, 0,90,90, 90,90, 90, 90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments if (iAngle != ORIENTATION_UNKNOWN) { int iNewOrientation = iLookup[iAngle / 15]; if (iOrientation != iNewOrientation) { iOrientation = iNewOrientation; // take action on new orientation here } } } }; // To display if orientation detection will work and enable it if (myOrientationEventListener.canDetectOrientation()) { Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show(); myOrientationEventListener.enable(); } else { Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show(); } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.