我想在文本文件中写入多个数据(移动加速度计读数)。 即所有读数,当我开始app时,直到我按下SAVE按钮。 我如何修改此代码来收集数据,这只会给出一次按下SAVE时X,Y,Z的读数。 这是显示读数的OnSensorChange函数:
public void onSensorChanged(SensorEvent event) {
acceleration.setText("X: " + event.values[0] +
"\n Y: " + event.values[1] +
"\n Z: " + event.values[2]
);
And here is saving file code :
String state;
state= Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state))
{
File Root= Environment.getExternalStorageDirectory();
File dir=new File(Root.getAbsolutePath()+"/myappfile");
if(!dir.exists()) {
dir.mkdir();
}
File file =new File(dir,"file.txt");
String Message=acceleration.getText().toString();
try {
FileOutputStream fileOutputStream=new
FileOutputStream(file);
fileOutputStream.write(Message.getBytes());
fileOutputStream.close();
acceleration.setText("");
Toast.makeText(getApplicationContext(),"SAVED",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
Toast.makeText(getApplicationContext(),"not found",Toast.LENGTH_LONG).show();
}