判断设备方向是否被锁定(检测是否自动旋转enabled/disabled)

Find out if device orientation is locked (Detect if auto-rotate is enabled/disabled)

如何确定设备的屏幕方向是否已锁定?我正在使用 OrientationEventListener 在我的应用程序中触发一些操作,如果用户的屏幕已锁定,我想禁用这些操作。

我知道我通常可以这样定位,但是如何找出这个锁定的方向:

    int orientation = getResources().getConfiguration().orientation;

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        // It's portrait
    } else {
        // It's landscape
    }

使用这个:

if (android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
        Toast.makeText(getApplicationContext(), "Auto Rotate is ON", Toast.LENGTH_SHORT).show();

    }
    else{
        Toast.makeText(getApplicationContext(), "Auto Rotate is OFF", Toast.LENGTH_SHORT).show();
    }

您可以在运行时检查方向,例如:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();        
}
}

希望对您有所帮助。