1

I have an activity fixed on the manifest to Landscape mode, and I need to detect if the screen rotated to Portrait. for now, I'm using OrientationEventListener and play with and put conditions on Orientation values, but I feel that it's not consistent and that there is too much cases to handle, and I'll need to keep track of all orientations values to really keep track of all the passed values, I kinda feel that I'm reinventing the wheel..

private void listenToDeviceRotation () {
    OrientationEventListener mOrientationEventListener = new OrientationEventListener (CodesActivity.this) {

        @Override
        public void onOrientationChanged (int orientation) {
            if (orientation == ORIENTATION_UNKNOWN) {
                return;
            }

            Log.e ("-- " + CodesActivity.class.getSimpleName () + " - ", "ORIENTATION : " + orientation);

            if (firstOrientation == -1) {
                firstOrientation = orientation;
                return;
            }

            if (((orientation >= 0 && orientation < 120) || (orientation > 240 && orientation < 360))) {
                finish();
                return;
            }
        }
    };

    if (mOrientationEventListener.canDetectOrientation()){
        mOrientationEventListener.enable();
    }
}

Is there any better way to get screen rotation/orientation even though the Activity is fixed to Landscape ?

ps : I target API 17.

Thanks.

2 Answers2

0

This is what I ended up doing (I'm sure it could and should be improved, I had to come up with something fast enough before the deadline :) )

private final static int LeftMin        = 240;
private final static int LeftMax        = 360;
private final static int LeftRotFirer   = 340;
private final static int RightMin       = 0;
private final static int RightMax       = 120;
private final static int RightRotFirer  = 20;

private int firstOrientation = -1;

private List<Integer> passedPositions;

private void listenToDeviceRotation () {
    orientationEventListener = new OrientationEventListener (CodesActivity.this) {

        @Override
        public void onOrientationChanged (int orientation) {
            if (orientation == ORIENTATION_UNKNOWN) {
                return;
            }

            rotationHandler (orientation);
        }
    };

    if (orientationEventListener.canDetectOrientation ()){
        orientationEventListener.enable ();
    }
}

private void rotationHandler (int orientation) {

    if (firstOrientation == -1) {
        firstOrientation = orientation;
        return;
    }

    if (firstOrientation > LeftMin && firstOrientation < LeftMax) {
        if (orientation > LeftRotFirer || orientation < RightRotFirer) {
            orientationEventListener.disable ();
            finish ();
            return;
        }
    }

    passedPositions.add (orientation);

    if (firstOrientation > RightMin && firstOrientation < RightMax) {
        if (orientation > LeftRotFirer || orientation < RightRotFirer) {
            for (int i = 0; i < passedPositions.size (); i ++) {
                int passedOrientation = passedPositions.get (i);
                if (passedOrientation < LeftRotFirer && passedOrientation > LeftMin) {
                    finish ();
                    orientationEventListener.disable ();
                    return;
                }
            }
        }
    }
}
-1

In your orientation changed listener just do this:

getResources().getConfiguration().orientation;

I assume you just want to know if the user's phone is in portrait mode? Without physically changing the orientation.

You will receive either ORIENTATION_PORTRAIT or ORIENTATION_LANDSCAPE.

Have a look at the answers provided here as well.

Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
  • 1
    It doesn't work. The `getResources().getConfiguration().orientation` will always return what's declared on the manifest. –  Jan 21 '16 at 11:56