嘿,伙计们,我目前正在开展一个项目,该项目将访问加速度计并将其绘制成我的代码当前无法正常工作的情况,我不确定原因,任何帮助将不胜感激。
该程序需要记录数据 30 秒,然后输出一个图表,但它始终为所有 3 个轴输出 0 值。
编码:
public class AccelerometerData extends Activity implements SensorEventListener {
public float _x;
public float _y;
public float _z;
boolean senseA = false;
int Time = 30;
int samplerate=10;
int sampleSize=(Time*1000)/samplerate;
public double[] X = new double[sampleSize];
public double[] Y = new double[sampleSize];
public double[] Z = new double[sampleSize];
Sensor accelerometer;
SensorManager sm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
final Timer updateTimer = new Timer();
updateTimer.scheduleAtFixedRate(new TimerTask(){
public void run(){
if (senseA= true){
for (int i = 0; i < sampleSize; i++){
X[i] = _x;
Y[i] = _y;
Z[i] = _z;
}
}
}
}, 0, samplerate);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
_x = event.values[0];
_y = event.values[1];
_z = event.values[2];
senseA = true;
}
}
}
输出值如下:
AccelerometerData data= new AccelerometerData();
double[] dataX = data.X;
double[] dataY = data.Y;
double[] dataZ = data.Z;
int length = data.sampleSize;
提前感谢您的时间