参照sample做了一个sensor demo
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorEventListener lsn = new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
x = e.values[SensorManager.DATA_X];
y = e.values[SensorManager.DATA_Y];
z = e.values[SensorManager.DATA_Z];
Tvx.setText("X轴:" + x + "");
Tvy.setText("Y轴:" + y + "");
Tvz.setText("Z轴:" + z + "");
}
public void onAccuracyChanged(Sensor s, int accuracy) {
}
};
// 注册listener,第三个参数是检测的精确度
sensorMgr
.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);