问题
I am working on normal activity based application. I have turned off device orientation from device settings. When I run my app on device, the app changes orientation based on device movement. I have no clue how can an app rotate even when device orientation is locked. There is no such instance/issue reported or ever asked on stack overflow or any other forum. Please point out, what I might be missing?
回答1:
It was very silly mistake, but I thought of sharing here in case any one else faces this issue. At a point, in my application, I was setting device orientation programmatically to SCREEN_ORIENTATION_FULL_SENSOR:
activityContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
This allows app to change orientation, based on sensor, even when user has locked the device orientation.
回答2:
You could check if device rotation is off or on and then lock it, like this:
if (android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
//Auto Rotate is on, so don't lock
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
else{
//Auto Rotate is off, so lock
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
来源:https://stackoverflow.com/questions/45983438/app-auto-rotates-even-with-device-orientation-locked