我正在使用加速度传感器来检测我的设备是否平放在桌子上。奇怪的是,即使当我将手机放平或旋转时,它的值始终在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