It looks like you need a code that only responds when the deviceβs orientation changes to one of four normal orientations, and not at every angle. This will allow you to filter the orientation only at values ββof 0, 90, 180 and 270 degrees:
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(); }