getRotation always returns zero

强颜欢笑 提交于 2019-12-07 11:07:01

问题


getRotation always returns zero regardless which way the screen is oriented. I'm running this on a real device running 2.3. Any reason why?

 Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 int rotation = display.getRotation();

回答1:


getRotation will only work if the app has a UI and the UI is allowed to rotate. There is a setting for the operating system that can prevent any app from automatically rotating:

Settings > Display > Auto-rotate Screen

This has to be enabled. On my device it was disabled. Nevertheless, the Android docs on getRotation not only fail to point this out but even give you the assumption that getRotation will return the rotation even without a UI. You need a UI. This of course is a problem for something like a service that has no UI but needs to know the orientation of the device for purposes that have nothing to do with user interaction. Personally, I get continuously pissed by Google for hiring amateur programmers who think that accessing hardware features should almost always require some kind of UI. This kind of problem has popped up on many occassions, like having to have a SurfaceView to record a video.

Finally, the getRotation is clearly not required by an app to figure out whether the device is rotated 0, 90, 180 or 270 degrees. With Auto-Rotate Screen turned off, the default camera app is still able to figure out the angular rotation, so it cannot be from getRotation, unless it is somehow capable of turning on the Auto-Rotate Screen setting.



来源:https://stackoverflow.com/questions/14626066/getrotation-always-returns-zero

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