我有以下类可以从中读取数据accelerometer
:
public class Accelerometer implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
float deltaX;
float deltaY;
float deltaZ;
Activity activity;
public Accelerometer(Activity act)
{
this.activity = act;
mSensorManager = (SensorManager) this.activity.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
public float getDeltaX()
{
return this.deltaX;
}
public float getDeltaY()
{
return this.deltaY;
}
public float getDeltaZ()
{
return this.deltaZ;
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
deltaX = event.values[0];
deltaY = event.values[1];
deltaZ = event.values[2];
}
}
我正在activity
使用以下代码从我的主类访问这个类:
Accelerometer sbt = new Accelerometer (this);
tvX.setText(Float.toString(sbt.getDeltaX()) + " " +Float.toString(sbt.getDeltaY()) + " "+Float.toString(sbt.getDeltaZ()));
但是,它总是向我显示 0、0、0。不确定当前代码有什么问题。任何帮助将不胜感激。