App auto rotates even with device orientation locked

亡梦爱人 提交于 2019-12-24 10:42:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!