陀螺仪代码不显示值

时间:2016-11-01 16:09:00

标签: android gyroscope

这是一个简单的代码,用于从陀螺仪中获取X,Y和Z轴的值,并在三个文本框中显示它们但不产生任何输出。文本框保持不变。我错过了什么?

public class Orientation extends AppCompatActivity implements SensorEventListener {
TextView textX, textY, textZ;
SensorManager sensorManager;
Sensor sensor;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_orientation);

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);


    textX = (TextView) findViewById(R.id.textX);
    textY = (TextView) findViewById(R.id.textY);
    textZ = (TextView) findViewById(R.id.textZ);
}

public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

public void onStop() {
    super.onStop();
    sensorManager.unregisterListener(this);
}


public void onAccuracyChanged(Sensor sensor, int acc) {
}

public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];


    textX.setText("X : " + (int) x + " rad/s");
    textY.setText("Y : " + (int) y + " rad/s");
    textZ.setText("Z : " + (int) z + " rad/s");
}

};

编辑: 首先需要检查传感器是否存在。

1 个答案:

答案 0 :(得分:1)

解决方案非常蹩脚。手机没有陀螺仪。