2013-04-18 14 views
1

開啓這是我的手機是否被轉頭或不的Android如何在手機周圍

private SensorManager sensorManager; 
    private int orientationLim = 165; 

@Override 
    public void onSensorChanged(SensorEvent event) { 
     if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { 

      // If shake to stop is enabled 
      boolean turnAroundToStop = Utils.getBooleanFromProperties(this, Properties.SP_CB_TURN_AROUND_TO_STOP); 

      if (turnAroundToStop) { 
       float value = Math.abs(event.values[1]); 
       if (value > orientationLim && !stopped) { 
        // Down 
        stopped = true; 
       } else { 
        // Up 
        stopped = false; 
       } 
      } 
     } 
    } 

,但問題是,stopped變量設置爲true,即使手機沒有完全轉過身來,但檢測代碼檢測只是一點點。

我該如何修改此代碼,該代碼僅在手機中繼轉身時纔會執行。

回答

0
float pitch = values[2]; 
if (pitch <= 45 && pitch >= -45) { 
    // mostly vertical 
} else if (pitch < -45) { 
    // mostly right side up 
} else if (pitch > 45) { 
    // mostly left side up 
} 

到這裏看看關於這一主題:

http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/

+0

雖然這在理論上可以回答這個問題,[但最好(http://meta.stackexchange.com/q/ 8259)在這裏包括答案的重要部分,並提供參考鏈接。 – Nanne

+0

屏幕朝下水平怎麼樣? – senzacionale