如何获取Android设备的CURRENT方向(ActivityInfo.SCREEN_ORIENTATION_ *)?

我想找出一个设备的详细方向,最好从ActivityInfo或等效的SCREEN_ORIENTATION_LANDSCAPESCREEN_ORIENTATION_PORTRAITSCREEN_ORIENTATION_REVERSE_LANDSCAPESCREEN_ORIENTATION_REVERSE_PORTRAIT

包括StackOverflow中的一些答案

 getWindowManager().getDefaultDisplay().getRotation() 

但是这并不能告诉我该设备是处于纵向模式还是横向模式,而只是参照其自然位置旋转的方式 – 反过来,它可以是风景或肖像。

 getResources().getConfiguration().orientation 

返回以下三个之一: ORIENTATION_LANDSCAPEORIENTATION_PORTRAITORIENTATION_SQUARE ,然后不真正告诉我手机转向哪个方向(无论是颠倒还是转向哪个方面)。

我知道我可以将后者与DisplayMetrics结合使用,以查明设备的自然方向,但真的没有更好的方法吗?

我结束了使用以下解决scheme:

 private int getScreenOrientation() { int rotation = getWindowManager().getDefaultDisplay().getRotation(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; int orientation; // if the device's natural orientation is portrait: if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) { switch(rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; case Surface.ROTATION_270: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; default: Log.e(TAG, "Unknown screen orientation. Defaulting to " + "portrait."); orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; } } // if the device's natural orientation is landscape or if the device // is square: else { switch(rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; case Surface.ROTATION_270: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: Log.e(TAG, "Unknown screen orientation. Defaulting to " + "landscape."); orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; } } return orientation; } 

注意 :一些用户(下面的评论中的Geltrude和Holtaf)指出,由于自然方向的旋转方向不是标准化的,所以这个解决scheme不能在所有设备上工作。

简单的方法是使用

 getResources().getConfiguration().orientation 

1是Potrait,2是Landscape。

 public static int getScreenOrientation(Activity activity) { int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int orientation = activity.getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_PORTRAIT) { if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) { return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } else { return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } } if (orientation == Configuration.ORIENTATION_LANDSCAPE) { if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) { return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } else { return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } } return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; } 

getResources().getConfiguration().orientation是了解当前使用getResources().getConfiguration().orientation的标准方法。 但是,如果它不能满足你的需求,那么也许你可以使用传感器来计算它的angular度。 阅读这个和这个

我认为你的问题是,你可以检测到风景和肖像,但不能逆转风景和反向protrait,因为它们不支持旧版本。 要检测你能做的是,你可以使用oreintation和旋转。 我给你一个想法可能对你有用。

试试这个我认为这可能会解决你的问题。

  int orientation = getResources().getConfiguration().orientation; int rotation = getWindowManager().getDefaultDisplay().getRotation(); int actual_orientation = -1; if (orientation == Configuration.ORIENTATION_LANDSCAPE && (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)){ orientation = Configuration.ORIENTATION_LANDSCAPE; } else if (orientation == Configuration.ORIENTATION_PORTRAIT && (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)) { orientation = Configuration.ORIENTATION_PORTRAIT; } else if (orientation == Configuration.ORIENTATION_LANDSCAPE && (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)){ orientation = //any constant for reverse landscape orientation; } else { if (orientation == Configuration.ORIENTATION_PORTRAIT && (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)){ orientation = //any constant for reverse portrait orientation; } } 

我结束了使用Zoltán的答案,除了当我在平板电脑(三星P6210 Galaxy Tab 7.0 Plus)上试用时,这很好用。 在肖像模式下,它返回SCREEN_ORIENTATION_REVERSE_PORTRAIT。 所以在else语句中(如果自然方向是横向的),我换了ROTATION_90和ROTATION_270的情况,似乎一切正常。 (我没有足够的声望发表这个评论佐尔坦的答案。)

你可以用一个非常简单的方法来实现:获得屏幕的widhtheight 。 设备处于横向时,屏幕宽度将始终较高。

 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); Toast.makeText(getApplicationContext(), "" + width + "," + height, Toast.LENGTH_SHORT).show(); if (width > height) { Toast.makeText(getApplicationContext(), "LandScape", Toast.LENGTH_SHORT).show(); } 

这是否解决您的问题?

 public static int getscrOrientation(Activity act) { Display getOrient = act.getWindowManager() .getDefaultDisplay(); int orientation = getOrient.getOrientation(); // Sometimes you may get undefined orientation Value is 0 // simple logic solves the problem compare the screen // X,Y Co-ordinates and determine the Orientation in such cases if (orientation == Configuration.ORIENTATION_UNDEFINED) { Configuration config = act.getResources().getConfiguration(); orientation = config.orientation; if (orientation == Configuration.ORIENTATION_UNDEFINED) { // if height and widht of screen are equal then // it is square orientation if (getOrient.getWidth() == getOrient.getHeight()) { orientation = Configuration.ORIENTATION_SQUARE; } else { // if widht is less than height than it is portrait if (getOrient.getWidth() < getOrient.getHeight()) { orientation = Configuration.ORIENTATION_PORTRAIT; } else { // if it is not any of the above it will defineitly // be landscape orientation = Configuration.ORIENTATION_LANDSCAPE; } } } } return orientation; // return value 1 is portrait and 2 is Landscape // Mode }