简体   繁体   English

从加速计传感器写入文件 - android

[英]Writing from accelerometer sensor to a file - android

I'm facing a technician code problem. 我正面临技术人员代码问题。 I want to write all the values I get from TotalAccelerate into my .txt file through Toggle button. 我想通过Toggle按钮将从TotalAccelerate获得的所有值写入我的.txt文件。 the code is now writing only one value at a time once I toggle the button. 我切换按钮后,代码现在一次只写一个值。 How can I manually write and stop writing into the file? 如何手动编写并停止写入文件? Thanks in advance. 提前致谢。

FileOutputStream fileOutputStream;
double TotalAccelerate; //I made it global variable
ArrayList<Double> list;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = new ArrayList<Double>();

     //for Accelermeter
    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sensorText = (TextView) findViewById(R.id.sensor);
    accelermeter = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sm.registerListener(this, accelermeter, SensorManager.SENSOR_DELAY_NORMAL);
    mDetector = new GestureDetectorCompat(this, new MyGestureListener());


    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File Root = Environment.getExternalStorageDirectory();
        File dir = new File(Root.getAbsolutePath() + "/MyApp");
        if (!dir.exists()) {
            dir.mkdir();
        }
        File file = new File(dir, "MyMessage.txt");
        try {
            fileOutputStream = new FileOutputStream(file, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    } else {
        Toast.makeText(getApplicationContext(), "SDcard not found", Toast.LENGTH_LONG).show();
    }

    OnStore = (ToggleButton) findViewById(R.id.onStore);
    OnStore.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (OnStore.isChecked()){
                try {
                    for(TotalAccelerate : list){
                         String space = "\n";
                        byte[] convert = space.getBytes();
                        fileOutputStream.write(convert);
                        String finalData;
                        finalData = String.valueOf(TotalAccelerate);
                        fileOutputStream.write(finalData.getBytes());
                    }
                     Toast.makeText(getApplicationContext(), "Message saving", Toast.LENGTH_LONG).show();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }if (!OnStore.isChecked()){
                try {
                    fileOutputStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fileOutputStream.close();
                     //added
                    list.clear();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(getApplicationContext(),"Message Stopped.",Toast.LENGTH_LONG).show();

            }


        }

    });
}// end OnCreate method

//Find the total acccerleration from the three sensors, 
  then write the values into a file for off-line analysis
@Override
public final void onSensorChanged(SensorEvent event) {
    // The light sensor returns a single value.
    // Many sensors return 3 values, one for each axis.
    double xx = event.values[0];
    double yy = event.values[1];
    double zz = event.values[2];
    TotalAccelerate = Math.round(Math.sqrt(Math.pow(xx, 2)
            + Math.pow(yy, 2)
            + Math.pow(zz, 2)));
    Log.i(DEBUG, "Accelerometer = " + TotalAccelerate);

    //  list = new ArrayList<Double>();
    list.add(TotalAccelerate);
    findPeaks(list);
    sensorText.setText("Total: " + list);
    Log.i(DEBUG, "Total " + list);

}

You are creating a new List each time the sensor changes, with the line:- 每次传感器更改时,您都会创建一个新列表,其中包含以下行: -

list = new ArrayList<Double>();

Move this to the start of your program, and empty the list once you have written it to the file. 将其移动到程序的开头,并在将列表写入文件后清空列表。 You will also need to synchronize the places where the list is modified. 您还需要同步修改列表的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM