【问题标题】:Getting the actual orientation of the phone获取手机的实际方向
【发布时间】:2011-11-30 02:01:28
【问题描述】:

我需要确定运行我的应用程序的手机的实际方向,但我找不到任何方法。我找到了“getRequestedOrientation”函数,但它只检索“setRequestedOrientation”函数设置的方向。

在我的应用程序中,我让手机根据其物理方向选择方向。根据结果​​,我还有一些定制要做。

我该怎么办?

提前感谢您花时间帮助我。

【问题讨论】:

标签: java android orientation


【解决方案1】:

根据这两个问题:

你可以使用:

Activity.getResources().getConfiguration().orientation

然而,根据第二个问题的评论,这个值只返回景观 或肖像。如果您也需要反向定位,我建议您查询加速度计 值来检测差异。但是,我不熟悉 Android 和 加速度计足以建议如何做到这一点。

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      它真的是为了得到当前的方向。

      Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
      int width = display.getWidth();
      int height = display.getHeight();
      int orientation = display.getOrientation();
      

      【讨论】:

      • 更简单... getResources().getConfiguration().orientation
      • 这个答案让我上路了,但“getSystemService”返回了一个空句柄。我改用了“getWindowManager”,它可以工作。
      【解决方案4】:

      请参阅 thisthis,this stackoverflow 问题。它们都解决了如何获得 Android 手机方向,第二个包括对 Android 指南针和方向系统如何工作的详细说明。

      【讨论】:

        猜你喜欢
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多