Android传感器旋转角度

时间:2013-04-16 18:52:33

标签: java android rotation sensor

我正在尝试检测y轴旋转角度(在横向保持时向前 - 向后倾斜)但我显然做错了因为onSensorChanged事件永远不会被触发

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ...

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
    mSensorManager.registerListener(this.mSensorEventListener, mSensor, SensorManager.SENSOR_DELAY_GAME); 
}

...

private SensorEventListener mSensorEventListener = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float aX = event.values[1];
        float aY = event.values[2];

        double angle = aY * (Math.PI / 180);
        btnStop.setText(Double.toString(angle));
};

1 个答案:

答案 0 :(得分:0)

您的设备可能没有Sensor.TYPE_ROTATION_VECTOR