调用`setRequestedOrientation`后查找设备方向更改的方法

时间:2013-08-06 11:20:53

标签: android android-orientation

在我们致电setRequestedOrientation后,Android中是否有任何方式可以了解设备方向的变化,我尝试过以下方法。

  1. getResources().getConfiguration().orientation
  2. OrientationEventListener
  3. Display getOrient = getWindowManager().getDefaultDisplay().getRotation();
  4. 但即使更改了设备方向,上述所有方法都会给出相同的值 在我们致电setRequestedOrientation之后,有人可以建议我们通过某种方式找到设备方向更改。

2 个答案:

答案 0 :(得分:3)

可能是变通方法:处理从OrientationEventListener添加到currente上下文的onOrientationChanged度。例如:

final OrientationEventListener orientationEventListener = new OrientationEventListener( getApplicationContext() ) {

    @Override
    public void onOrientationChanged( final int orientation ) {
        Log.i( "orientation_test", "orientation: " + orientation );
    }
};

orientationEventListener.enable();

在onResume上添加并启用它,并在onPause of Activity中禁用,例如。

答案 1 :(得分:-1)

这应该有效:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
    // Do something
else
    // Do something else
相关问题