如果活动在横向模式下固定,则检测旋转为纵向

时间:2016-01-21 11:33:37

标签: android rotation listener landscape

我在清单上修复了一个活动到横向模式,我需要检测屏幕是否旋转到纵向。 现在,我使用OrientationEventListener并在方向值上玩和设置条件,但我觉得它不一致,并且有太多的情况要到处理,我需要跟踪所有的方向值,以真正跟踪所有传递的值,我觉得我重新发明轮子..

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();
    }
}

即使将“活动”固定为“横向”,还有更好的方法来获得屏幕旋转/方向吗?

ps:我的目标是API 17。

感谢。

2 个答案:

答案 0 :(得分:0)

这就是我最终要做的事情(我确信它可以并且应该改进,我必须在截止日期之前提出足够快的东西:))

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 :(得分:-1)

在你的方向改变了听众只需这样做:

getResources().getConfiguration().orientation;

我假设您只是想知道用户的手机是否处于纵向模式?无需实际更改orientation

您将收到ORIENTATION_PORTRAITORIENTATION_LANDSCAPE

还要看看here提供的答案。