.txt
基本的に、センサーデータをSDカードのファイルに書き込もうとしています。
(int i = 0; i % 2 == 0; i++) を設定すると、完全に正常に動作します。つまり、.txt
2 サンプルごとにファイルにデータを書き込みます。
しかし、このコードに示されているように、5 を 1 に変更しました。つまり、すべてのサンプルをファイルに書き込む必要があります。実行すると、UI がフリーズします。
誰でもこれを解決するのを手伝ってもらえますか?
別のスレッドを作成することで修正できますか? (そう言うのは正確ですか?)
私は新しいので、スレッドの問題が原因である可能性があることを大まかに知っているだけです。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
EditText txtData;
Button startButton;
Button stopButton;
File myFile;
FileOutputStream fOut;
OutputStreamWriter myOutWriter;
BufferedWriter myBufferedWriter;
PrintWriter myPrintWriter;
private SensorManager sensorManager;
private long currentTime;
private long startTime;
float[] acceleration = new float[3];
float[] rotationRate = new float[3];
float[] magneticField = new float[3];
boolean stopFlag = false;
boolean startFlag = false;
boolean isFirstSet = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// file name to be entered
txtData = (EditText) findViewById(R.id.editText2);
txtData.setHint("Enter File Name here...");
// start button
startButton = (Button) findViewById(R.id.button1);
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// start recording the sensor data
try {
myFile = new File("/sdcard/ResearchData/" + txtData.getText() + ".txt");
myFile.createNewFile();
fOut = new FileOutputStream(myFile);
myOutWriter = new OutputStreamWriter(fOut);
myBufferedWriter = new BufferedWriter(myOutWriter);
myPrintWriter = new PrintWriter(myBufferedWriter);
Toast.makeText(getBaseContext(), "Start recording the data set", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
startFlag = true;
}
}
});
// stop button
stopButton = (Button) findViewById(R.id.button2);
stopButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// stop recording the sensor data
try {
stopFlag = true;
Toast.makeText(getBaseContext(), "Done recording the data set", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (startFlag) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
acceleration[0] = event.values[0];
acceleration[1] = event.values[1];
acceleration[2] = event.values[2];
}
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
rotationRate[0] = event.values[0];
rotationRate[1] = event.values[1];
rotationRate[2] = event.values[2];
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
magneticField[0] = event.values[0];
magneticField[1] = event.values[1];
magneticField[2] = event.values[2];
}
if (isFirstSet) {
startTime = System.currentTimeMillis();
isFirstSet = false;
}
currentTime = System.currentTimeMillis();
for (int i = 0; i % 1 == 0; i++) {
if (!stopFlag) {
save();
}
else {
try {
myOutWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
private void save() {
myPrintWriter.write(currentTime - startTime + " " + acceleration[0] + " " + acceleration[1] + " " + acceleration[2]
+ " " + rotationRate[0] + " " + rotationRate[1] + " " + rotationRate[2]
+ " " + magneticField[0] + " " + magneticField[1] + " " + magneticField[2] + "\n");
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the sensors
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
}