Android:从服务获取当前设备方向

问题描述 投票:3回答:2

此问题涉及3d几何形状和对Android传感器的扎实了解。我花了数周时间寻找无法成功的解决方案,并感谢社区的帮助。

是否可以通过Android中的服务获取当前设备方向(人像或风景)?我不需要来自传感器的持续更新;只是当前设备的方向。即使启动器是纵向的(即使用户将自动旋转设置为关闭),当设备处于横向时,设备方向也应正确报告。

示例代码/示例代码将受到高度赞赏。

谢谢。

android orientation screen-orientation android-orientation device-orientation
2个回答
2
投票

是的,可以

WindowManager windowService = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int currentRatation = windowService.getDefaultDisplay().getRotation();

if (Surface.ROTATION_0 == currentRatation) {
   currentRatation = PORT;
} else if(Surface.ROTATION_180 == currentRatation) {
   currentRatation = PORT;
} else if(Surface.ROTATION_90 == currentRatation) {
   currentRatation = LAND;
} else if(Surface.ROTATION_270 == currentRatation) {
   currentRatation = LAND;
}

0
投票

您可以在下面检查正在使用的资源

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//
} else {
//
}
© www.soinside.com 2019 - 2024. All rights reserved.