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

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

概述基本上,我正在尝试将传感器数据写入SD卡中的.txt文件.当我设置为(inti=0;i%2==0;i)时,它完全正常工作,即每2个样本将数据写入.txt文件.但是如此代码所示,我将5更改为1,即我希望每个样本都写入文件中.一旦我运行它,UI就会冻结.有人可以帮我解决这个问题吗?可以通过创建另一

基本上,我正在尝试将传感器数据写入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 – 将传感器读数写入.txt文件.用户界面冻结全部内容,希望文章能够帮你解决Android – 将传感器读数写入.txt文件.用户界面冻结所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存