Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Write sensor readings into .txt file. UI freezes
    primarykey
    data
    text
    <p>Basically, I'm trying to write the sensor data into a <code>.txt</code> file in my SD card.</p> <p>It works perfectly fine, when I set for (int i = 0; i % 2 == 0; i++), i.e., write the data into the <code>.txt</code> file every 2 samples. </p> <p>But as shown in this code, I changed 5 to 1, i.e., I want every single sample to be written in the file. Once I run it, the UI freezes.</p> <p>Anybody can help me to solve this?</p> <p>Can it be fixed by creating another thread? (Is it accurate to say so?)</p> <p>I'm new and thus only roughly know maybe the problem is due to the thread issue.</p> <pre><code>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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload