其他分享
首页 > 其他分享> > Android – 将传感器读数写入.txt文件.用户界面冻结

Android – 将传感器读数写入.txt文件.用户界面冻结

作者:互联网

基本上,我正在尝试将传感器数据写入SD卡中的.txt文件.

当我设置为(int i = 0; i%2 == 0; i)时,它完全正常工作,即每2个样本将数据写入.txt文件.

但是如此代码所示,我将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

    }
}

解决方法:

如果您将代码更改为:

for(int i=0;i<1;i++){
    //your code to save
}

在你的代码中,如果你设置条件i%1 == 0,你循环进入无限循环……

标签:android,multithreading,android-lifecycle
来源: https://codeday.me/bug/20190831/1779037.html