我编写了以下几行,通过单击按钮将加速度计数据记录在文件中
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final SensorEventListener mySensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
xAxis_lateralA = sensorEvent.values[0];
yAxis_longitudinalA = sensorEvent.values[1];
zAxis_verticalA = sensorEvent.values[2]; // TODO apply the acceleration changes to your application.
textView.append("\nACC_x = "+ xAxis_lateralA + ", ACC_y = "+yAxis_longitudinalA+ ", ACC_z = " + zAxis_verticalA);
acc+="\n"+xAxis_lateralA + ", "+ yAxis_longitudinalA+", "+zAxis_verticalA;
try {
File myFile = new File("/sdcard/acc.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(acc);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'acc.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
};
// write on SD card file data in the text box
sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
int sensorType = Sensor.TYPE_ACCELEROMETER;
sm.registerListener(mySensorEventListener,sm.getDefaultSensor(sensorType), SensorManager.SENSOR_DELAY_NORMAL);
}// onClick
});
现在我希望它通过单击另一个按钮来停止记录数据。例如 -
stopButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int sensorType = Sensor.TYPE_ACCELEROMETER;
sm.unregisterListener(listener, sensor)
}// onClick
}); // btnstopButton
}
想使用unregisterListener
,但大多数时候它说不推荐使用。
有人可以帮忙吗?