【问题标题】:Android: Get current device orientation from serviceAndroid:从服务获取当前设备方向
【发布时间】:2014-04-14 06:34:55
【问题描述】:

这个问题涉及 3d 几何和对 Android 传感器的深入了解。我花了数周时间寻找解决方案但没有成功,我希望得到社区的帮助。

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

示例/示例代码将不胜感激。

谢谢。

【问题讨论】:

  • 找到解决办法了吗?
  • 您设法找到了解决方案?

标签: android orientation screen-orientation android-orientation device-orientation


【解决方案1】:

是的,你可以

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;
}

【讨论】:

  • 在主屏幕顶部更改设备方向时,此功能不起作用。只有在更改设备屏幕方向时,此 api 才能正常工作。
【解决方案2】:

您可以通过以下方式查看服务中的资源

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//
} else {
//
}

【讨论】:

  • 在主屏幕顶部更改设备方向时,这不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-16
  • 2021-07-21
相关资源
最近更新 更多