【问题标题】:How to get Android device orientation when launch the app启动应用程序时如何获取 Android 设备方向
【发布时间】:2016-09-07 10:43:18
【问题描述】:

我试过这个,不行,我总是0度

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenOrientation = display.getRotation();

如果使用SensorManager,也是不行的,因为不移动手机也不会触发

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

    @Override
    public void onOrientationChanged(int orientation) {
    }
};

更新

请尝试完整的演示

https://github.com/sochap/getorientationquestdemo

由于显示没有变化,用显示判断是不可能的

我要获取设备方向,而不是显示方向

OrientationEventListener 可以,但onCreate 不行

【问题讨论】:

  • 检查屏幕方向 在 oncreate "if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { //Landscape mode }" 中写下这段代码,如果你想检查然后你也可以通过纵向模式查看
  • 您可以通过在Activity中重写onOrientationChaged来检查屏幕方向何时改变
  • getResources().getConfiguration().orientation 始终为 1
  • 无法用display判断,试试我的demo

标签: android android-sensors android-orientation device-orientation


【解决方案1】:

这可能对你有用。试试这个。

 Configuration config = getResources().getConfiguration();

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {

                     // set Values for landscape

 } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {

                   // set Values for portrait
    }

【讨论】:

    【解决方案2】:

    试试

    int currentOrientation = getResources().getConfiguration().orientation;
    
    if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
         //do something
    } else {
         //do something
    } 
    

    【讨论】:

      【解决方案3】:

      使用Activity的onConfigurationChanged方法。见以下代码:

      @Override
      public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
         // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
          Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
      }
      

      您还必须在清单文件中编辑适当的元素以包含 android:configChanges 只需查看以下代码:

      <activity android:name=".MyActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name">
      

      注意: 对于 Android 3.2(API 级别 13)或更高版本,当设备在纵向和横向之间切换时,“屏幕尺寸”也会发生变化。因此,如果您想在为 API 级别 13 或更高级别进行开发时防止由于方向更改而导致运行时重新启动,则必须为 API 级别 13 或更高级别声明 android:configChanges="orientation|screenSize"。

      希望这会对你有所帮助..:)

      【讨论】:

      • 这个方法永远不会被调用,因为我将android:screenOrientation="portrait"设置为活动,我也在Android中禁用了旋转
      • 试试这个:int rotation = getWindowManager().getDefaultDisplay() .getRotation();
      • int 方向; CharSequence 文本;开关(旋转){ case Surface.ROTATION_0: text = "SCREEN_ORIENTATION_PORTRAIT";休息; case Surface.ROTATION_90: text = "SCREEN_ORIENTATION_LANDSCAPE";休息; case Surface.ROTATION_180: text = "SCREEN_ORIENTATION_REVERSE_PORTRAIT";休息; case Surface.ROTATION_270: text = "SCREEN_ORIENTATION_REVERSE_LANDSCAPE";休息;默认值:文本 = "SCREEN_ORIENTATION_PORTRAIT";休息; }
      • 试试这些,如果您遇到任何问题,请告诉我
      • @CL 请提供您的演示链接
      猜你喜欢
      • 1970-01-01
      • 2020-02-10
      • 2015-10-28
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2011-03-25
      相关资源
      最近更新 更多