我在清单文件中将活动方向设置为竖屏,因为我不想让屏幕旋转。
但是当我点击按钮时,我想要获取Android设备的当前方向(竖屏或横屏)?我不需要从传感器获得连续更新;只需要在点击按钮时获取当前设备方向即可。
PS:设备方向是我拿着设备的方式。不要将其与屏幕方向混淆。
这是我目前所做的:
int PORT=1,LAND=2;
WindowManager windowService = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
int rotation = windowService.getDefaultDisplay().getRotation();
if (Surface.ROTATION_0 == rotation) {
rotation = PORT;
} else if(Surface.ROTATION_180 == rotation) {
rotation = PORT;
} else if(Surface.ROTATION_90 == rotation) {
rotation = LAND;
} else if(Surface.ROTATION_270 == rotation) {
rotation = LAND;
}
return rotation;
即使使用以下方法也无济于事。
getResources().getConfiguration().orientation
由于我的活动默认设置为纵向,因此它始终返回1。
非常感谢提供示例代码。
谢谢。