使用 getRotation 方法:
Display display = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
从文档中:
Returns the rotation of the screen from its "natural" orientation.
The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.
ROTATION_180, or Surface.ROTATION_270.
For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned.
The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device.
For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.
getRotation 是从 Android 2.2 引入的。如果您的目标是旧设备,请使用 getOrientation。
在这里得到答案:
如何检测android设备的方向?
如果您只想检测设备的方向,则可以使用 OrientationEventListener。
这是官方文档:
http: //developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int)
但在你使用任何东西之前,请查看这篇关于 android 中方向处理的优秀博文:
http ://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html