0

我这里好像有错误。我正在尝试更改设置中的加速度计自动旋转值。

现在,我设法锁定和解锁旋转设备。但是,每当我锁定设备时,它都会进入纵向模式,无论我在锁定它时处于什么方向。

这是我的代码:

    public void setAutoOrientationEnabled(boolean enabled)
{
  Settings.System.putInt(content, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

谢谢你的帮助!

4

2 回答 2

1

此代码示例锁定/解锁屏幕旋转并在锁定时保留当前方向:

if (Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1) {
            Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            Settings.System.putInt( context.getContentResolver(), Settings.System.USER_ROTATION, defaultDisplay.getRotation());
            Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);       
} else {
            Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);                
}
于 2014-11-08T09:55:28.207 回答
0
   int orientation = this.getRequestedOrientation();
        int rotation = ((WindowManager) this.getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        switch (rotation) {
            case Surface.ROTATION_0:
                Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, 1);
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.R`enter code here`OTATION_90:
                Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, 0);
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
        }

        this.setRequestedOrientation(orientation);
于 2017-09-05T08:39:54.860 回答