我需要检查手机是否处于水平位置(没有横向,我的意思是就像你将它放在 table 上一样)。
我知道我必须使用“SensorEventListener”接口(interface)和“onSensorChanged”事件,但我无法获取如何检查移动位置的示例。
@Override
public void onSensorChanged(SensorEvent event) {
//
}
最佳答案
试试这个:
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
float norm =(float) Math.sqrt(x * x + y * y + z * z);
// Normalize the accelerometer vector
x = (x / norm);
y = (y / norm);
z = (z / norm);
int inclination = (int) Math.round(Math.toDegrees(Math.acos(z)));
if (inclination < 25 || inclination > 155)
{
// device is horiontal
Toast.makeText(this,"device horiontal !",Toast.LENGTH_SHORT).show();
}
}
关于android - onSensorChanged 检查手机是否处于水平位置(无横向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49750948/