我好像在这里有错误。我正在尝试更改设置中的加速度计自动旋转值。
现在,我设法锁定和解锁旋转设备。但是,无论何时我锁定设备,它都会进入纵向模式,无论我在锁定它时的方向是什么。
这是我的代码:
public void setAutoOrientationEnabled(boolean enabled)
{
Settings.System.putInt(content, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
感谢您的帮助!
答案 0 :(得分: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);
}
答案 1 :(得分: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);