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

时间:2017-01-25 13:40:27

标签: java android android-studio fileoutputstream

我面临技术人员代码问题。我想通过切换按钮将我从 TotalAccelerate 获得的所有值写入我的.txt文件。我切换按钮后,代码现在一次只写一个值。如何手动编写并停止写入文件? 提前致谢。

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);

}

1 个答案:

答案 0 :(得分:0)

每次传感器更改时,您都会创建一个新的列表,其中包含以下行: -

list = new ArrayList<Double>();

将其移至程序的开头,并在将列表写入文件后清空列表。您还需要同步修改列表的位置。