检测设备方向是否被锁定(是否启用/禁用自动旋转)

7

如何判断设备的屏幕方向是否被锁定?我正在使用OrientationEventListener来触发应用程序中的一些操作,如果用户已经锁定了他的屏幕,我希望禁用这些操作。

我知道我通常可以像这样获取方向,但如何找出是否已锁定此方向:

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

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

27

使用此代码:

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

1
有没有办法检测“屏幕旋转”何时启用或禁用?我正在寻找一种类似广播的方式。 - Allen Vork

-2

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

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

希望能有所帮助。


1
谢谢Kristo,但这并没有提供屏幕是否已锁定的信息。 - Astagron
也许我误解了你的问题,因为你需要什么并不是很清楚。我以为你想检查方向的类型。无论如何,祝编码愉快。 - Kristo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接