簡體   English   中英

從加速計傳感器寫入文件 - android

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

我正面臨技術人員代碼問題。 我想通過Toggle按鈕將從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);

}

每次傳感器更改時,您都會創建一個新列表,其中包含以下行: -

list = new ArrayList<Double>();

將其移動到程序的開頭,並在將列表寫入文件后清空列表。 您還需要同步修改列表的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM