2013-02-19 49 views
7

您會認爲會有一個直接的解決方案。 Android文檔狀態:獲取Android設備的方向

在Android 2.2(API級別8)中不推薦使用方向傳感器。 我們建議您使用getRotationMatrix()方法與 getOrientation()方法來計算方向值,而不是使用方向傳感器中的原始數據,我們建議您使用 。

然而,他們沒有提供如何實施getOrientation()getRotationMatrix()的解決方案。我花了幾個小時閱讀了使用這些方法的開發人員的帖子,但他們都部分粘貼了代碼或一些奇怪的實現。谷歌搜索沒有提供教程。有人可以使用這兩種方法粘貼一個簡單的解決方案來生成方向?

+0

所以你想要設備c urrent方向? – 2013-02-19 11:22:39

+0

是的,但它必須獨立於實際的屏幕。服務應該能夠確定方向。 – AndroidDev 2013-02-19 12:03:32

+0

您想要確定設備是否處於縱向或橫向模式? – 2013-02-20 05:05:31

回答

6

這裏是getOrientation()實現:

public int getscrOrientation() 
    { 
     Display getOrient = 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 = 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 
    } 

和U也可以參考這個例子代表了使用這兩種方法:

 getOrientation and getRotationMatrix 

http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

+0

獲取我已經在代碼中使用的屏幕方向。我不相信這與基於傳感器獲取設備方向是一回事。事實上,我甚至在過去嘗試過,它不會在沒有屏幕的服務中工作。服務通常不會訪問屏幕,並且應該能夠從傳感器確定設備方向。 getOrientation和getRotationMatrix是必需的。 – AndroidDev 2013-02-19 12:00:09

+0

我試過了,但並沒有像預期的那樣給出結果。我剛剛從應用商店下載了一個應用,其中顯示了設備上的所有傳感器以及它們如何操作。一個稱爲方向傳感器,當您使用它時,該應用程序將顯示傳感器的正確工作方式。當您不移動設備時,值不應該改變(或者改變很小)。上面鏈接中的代碼並沒有給出任何可靠的結果,但是值遍佈整個地方。 – AndroidDev 2013-02-19 13:09:12

+0

我開始認爲可能放置一個斷點並在調試模式下運行可能會影響傳感器讀取的方式。我將嘗試在不進行調試的情況下運行它,並在更新時顯示值並查看會發生什麼。 – AndroidDev 2013-02-19 13:14:11

4
public int getScreenOrientation() { 


// Query what the orientation currently really is. 
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)    { 
    return 1; // Portrait Mode 

}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
    return 2; // Landscape mode 
} 
return 0; 
} 
2
protected void onResume() { 
    // change the screen orientation 
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
     setContentView(R.layout.portrait); 
    } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     setContentView(R.layout.landscape); 
    } else { 
     setContentView(R.layout.oops); 
    } 
}