2

My activity is locked on LANDSCAPE. But still I need to know the orientation of the device. So I have elected to use sensors. I have the following code

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
...
@Override
  public void onSensorChanged(SensorEvent event) {
    float[] values = event.values;

    // Movement
    float azimuth = values[0];
    float pitch = values[1];
    float roll = values[2];

    if ((-110 <= pitch && pitch <= -70) || (70 <= pitch && pitch <= 110)) {
      //PORTRAIT MODE
      portraitPitch = true;
      landscapePitch = false;
      Log.d(TAG, "portrait mode: pitch = " + pitch);
    } else if ((-20 <= pitch && pitch <= 20) || (-200 <= pitch && pitch <= -160) || (160 <= pitch && pitch <= 200)) {
      //LANDSCAPE MODE
      portraitPitch = false;
      landscapePitch = true;
      Log.d(TAG, "landscape mode : pitch = " + pitch);
    }

    if ((-20 <= roll && roll <= 20)) {
      //PORTRAIT MODE
      portraitRoll = true;
      landscapePitch = false;
      Log.d(TAG, "portrait mode: roll = " + roll);
    } else if ((-110 <= roll && roll <= -70) || (70 <= roll && roll <= 110)) {
      //LANDSCAPE MODE
      portraitRoll = false;
      landscapePitch = true;
      Log.d(TAG, "landscape mode : roll = " + roll);
    }

    if (portraitPitch && portraitRoll && !portrait) {
      portrait = true;
      landscape = false;
      rotateIconsToPortraitMode();
      Log.d(TAG, "portrait mode for icons: pitch = " + pitch + ", roll = " + roll);
    }

    if (landscapePitch && landscapeRoll && !landscape) {
      landscape = true;
      portrait = false;
      rotateIconsToLandscapeMode();
      Log.d(TAG, "landscape mode for icons: pitch = " + pitch + ", roll = " + roll);
    }
}

My code is not working reliably, in fact, the otateIconsToLandscapeMode() is never reached.

How do I use the information about azimuth, pitch, and roll to determine the PORTRAIT or LANDSCAPE orientation of the device?` I hope my question is specific enough to warrant a specific answer. Thanks.

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • Have you tried handling configuration change yourself? http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange - add `configChanges: orientation` to manifest, and implement `onConfigurationChanged`, then maybe you can get `newConfig.orientation` – wasyl Sep 28 '15 at 15:21
  • That does not work. Once orientation is locked, the config change event is meaningless. You have to use sensor. – Katedral Pillon Sep 28 '15 at 15:24
  • Then I'd suggest http://stackoverflow.com/questions/27614439/how-to-detect-my-screen-orientation-in-portrait-locked-screen-in-android – wasyl Sep 28 '15 at 15:33

2 Answers2

8

In oncreate instantiate an OrientationEventListener

OrientationEventListener orientationEventListener = new OrientationEventListener(this)
    {
        @Override
        public void onOrientationChanged(int orientation)
        {
            Log.d(TAG, "orientation = " + orientation);
        }
    }; 

    orientationEventListener.enable();

Using the orientation value you can determine whether the device in in Portrait or Landscape.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
1

If LANDSCAPE vs PORTRAIT orientation is all you need, then the pitch value should be sufficient. It may not be perfect depending on your specific needs. But from my understanding, you can simply use pitch. So you it exactly as you have it, just ignore roll.

Konsol Labapen
  • 2,424
  • 2
  • 15
  • 12