首页 > 解决方案 > SensorManager 仅适用于某些设备

问题描述

我有以下代码来获取设备的 x 和 y 角度,它在我的手机上运行良好,但在我的平板电脑(三星 Galaxy Tab e)上运行良好。我想知道是否有人知道是什么导致它在一台设备上工作而不是在另一台设备上工作。

我还确保两者都启用了屏幕旋转。我的假设是平板电脑缺少传感器,而我最想要的是一种解决方法。任何帮助将非常感激。提前致谢!

源代码:

double yAngle;
double xAngle;

@Override
protected void onResume() {
    super.onResume();
    sensorManager.unregisterListener(this);
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),RATE);
}

@Override
public void onSensorChanged(SensorEvent event) {
    float[] rotationMatrix;
    rotationMatrix = new float[16];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
    determineOrientation(rotationMatrix);
    lblY.setText(String.format("%.1f", yAngle));
    lblX.setText(String.format("%.1f", xAngle));

}

private void determineOrientation(float[] rotationMatrix){
    //CREATING FLOAT ARRAY OF ORIENTATION VALUES
    float [] orientationValues = new float[3];
    SensorManager.getOrientation(rotationMatrix, orientationValues);
    yAngle = Math.toDegrees(orientationValues[2]);
    xAngle = Math.toDegrees(orientationValues[1]);
}

标签: javaandroidandroid-studioandroid-sensorssensormanager

解决方案


您可以使用它adb shell pm list features来检查所有传感器和支持的其他功能。


推荐阅读