我正在使用加速度傳感器來檢測我的設備是否平放在桌子上。奇怪的是,即使當我將手機放平或旋轉時,它的值始終在90到100之間!這不應該糾正!我錯過了什麼嗎?這裏是我的代碼:如何知道Android設備是否在桌子上平坦
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float norm_Of_g =(float) Math.sqrt(x * x + y * y + z * z);
// Normalize the accelerometer vector
x = (x/norm_Of_g);
y = (y/norm_Of_g);
z = (z/norm_Of_g);
int inclination = (int) Math.round(Math.toDegrees(Math.acos(y)));
Log.i("tag","incline is:"+inclination);
if (inclination < 25 || inclination > 155)
{
// device is flat
Toast.makeText(this,"device flat - beep!",Toast.LENGTH_SHORT);
}
編輯:我使用此代碼:How to measure the tilt of the phone in XY plane using accelerometer in Android
您應該使用'log.i(,)'而不是'System.out'這是用於終端的 –
加速度計通常比人類更敏感。我不得不禁用我的筆記本電腦的加速度計,因爲即使當我按下按鍵時,它也會將不需要的輸入提供給許多應用程序。 – o11c
默認情況下'System.out'是行緩衝的,所以輸出只有在發送換行符時纔可見。 – o11c