【问题标题】:How can I always remember the latest value from each sensor and write them into a file?如何始终记住每个传感器的最新值并将它们写入文件?
【发布时间】:2019-02-02 04:47:56
【问题描述】:

我为我最后一年的项目开发了一个 Android 应用程序。此应用程序必须从加速度计、陀螺仪和磁力计收集传感器智能手机信息,并将其保存到文件中。到目前为止,我已经完成了所有工作,但是现在我对传感器的采样率频率有些问题,我不知道如何处理。

我尝试使用可运行的方法来延迟 10ms 的加速度计和陀螺仪和其他可运行的方法来延迟 100ms 的磁力计。我这样做是因为通过近似值,我猜 ACC 和 GYR 的值每 10 毫秒改变一次。而对于 MAG,此值每 100 毫秒更改一次。

我从未使用过这种可运行的方法,我是初学者。我的代码有效,它创建了文件,但它有 0 个字节,这意味着我的代码没有正确输入 RUNNABLE 方法。我应该改变什么?

package localhost.dev.liverpoolsensorsfusion;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class RecordDataActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private Sensor mGyroscope;
    private Sensor mMagnetometer;
    Handler handler10;
    Runnable runnable10;
    Handler handler100;
    Runnable runnable100;
    CheckBox checkAcc;
    CheckBox checkGyro;
    CheckBox checkMagne;
    TextView accX;
    TextView accY;
    TextView accZ;
    TextView magneX;
    TextView magneY;
    TextView magneZ;
    TextView gyroX;
    TextView gyroY;
    TextView gyroZ;
    String FILENAME;
    String content1;
    String content2;
    String content3;
    FileOutputStream out;
    Button startButton;
    Button stopButton;
    boolean startFlag = false;
    EditText textData;
    private Chronometer chronometer;
    private boolean running_chronometer;
    boolean isFirstSet = true;
    private long currentTime;
    private long startTime;

    float[] acceleration = new float[3];
    float[] gyroscope = new float[3];
    float[] magnetometer = new float[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_data);
        // define sensors
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        // define text views
        accX = (TextView) findViewById(R.id.raw_value_acc_x);
        accY = (TextView) findViewById(R.id.raw_value_acc_y);
        accZ = (TextView) findViewById(R.id.raw_value_acc_z);
        magneX = (TextView) findViewById(R.id.raw_value_magne_x);
        magneY = (TextView) findViewById(R.id.raw_value_magne_y);
        magneZ = (TextView) findViewById(R.id.raw_value_magne_z);
        gyroX = (TextView) findViewById(R.id.raw_value_gyro_x);
        gyroY = (TextView) findViewById(R.id.raw_value_gyro_y);
        gyroZ = (TextView) findViewById(R.id.raw_value_gyro_z);

        // define checkboxes
        checkAcc=(CheckBox)findViewById(R.id.checkBox);
        checkGyro=(CheckBox)findViewById(R.id.checkBox2);
        checkMagne=(CheckBox)findViewById(R.id.checkBox3);

        // file name to be entered
        textData = (EditText) findViewById(R.id.edit_text);
        textData.setHint("Enter File Name here...");

        // define chronometer
        chronometer = findViewById(R.id.chronometer);
        chronometer.setFormat("Recording: %s");
        chronometer.setBase(SystemClock.elapsedRealtime());

        // define start button
        startButton = (Button) findViewById(R.id.button_record);
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // create file
                FILENAME= textData.getText() + ".csv";
                if(!checkAcc.isChecked() && !checkGyro.isChecked() && !checkMagne.isChecked()) {
                    Toast.makeText(getBaseContext(), "Please select at least one sensor!",
                            Toast.LENGTH_LONG).show();
                }else if(FILENAME.equals(".csv")){
                    Toast.makeText(getBaseContext(), "Please insert a valid name for the file to be created!",
                            Toast.LENGTH_LONG).show();
                }else {
                    // set the recording button ON
                    startFlag = true;
                    // make the chronometer run
                    if (!running_chronometer) {
                        chronometer.setBase(SystemClock.elapsedRealtime());
                        chronometer.start();
                        running_chronometer = true;
                        checkAcc.setClickable(false);
                        checkGyro.setClickable(false);
                        checkMagne.setClickable(false);
                    }
                    // add screen message to confirm that the app is recording
                    try{
                        textData.getText().clear();
                        Toast.makeText(getBaseContext(), "Start recording the data set", Toast.LENGTH_SHORT).show();
                    }catch(Exception e) {
                        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    // Turn off the record button
                    startButton.setClickable(false);
                }
            }
        }); // starts button ends here

        // define stop button
        stopButton=(Button)findViewById(R.id.button_stop);
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // set the recording button OFF
                // Stop writing on file
                startFlag = false;
                isFirstSet = true;
                // if there is no file created state the following message
                if(FILENAME==null || FILENAME.equals(".csv")){
                    Toast.makeText(getBaseContext(), "There is no recording taken in this moment!",
                            Toast.LENGTH_LONG).show();
                }else{
                    // stop the chronometer
                    chronometer.stop();
                    running_chronometer = false;
                    checkAcc.setClickable(true);
                    checkGyro.setClickable(true);
                    checkMagne.setClickable(true);
                    // add screen message to confirm that the app has saved the data set
                    try{
                        Toast.makeText(getBaseContext(), "Saved to " + getFilesDir() + "/" + FILENAME,
                                Toast.LENGTH_LONG).show();
                    }catch(Exception e) {
                        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                // Turn back on the Record button
                startButton.setClickable(true);
            }
        });  // stop button ends here
    } // onCreate class ends here

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {
        // when recording is ON do this
        if (startFlag) {
            if (checkAcc.isChecked() && event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                acceleration[0] = event.values[0];
                acceleration[1] = event.values[1];
                acceleration[2] = event.values[2];
            }

            if (checkGyro.isChecked() && event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                gyroscope[0] = event.values[0];
                gyroscope[1] = event.values[1];
                gyroscope[2] = event.values[2];
            }

            if (checkMagne.isChecked() && event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                magnetometer[0] = event.values[0];
                magnetometer[1] = event.values[1];
                magnetometer[2] = event.values[2];
            }


            // initialise the content that will be written in the file
            if (isFirstSet) {
                startTime = System.currentTimeMillis();
                isFirstSet = false;
            }

            currentTime = System.currentTimeMillis();

            content1 = (currentTime - startTime) + "," + "ACC" + "," + acceleration[0] + "," + acceleration[1] + "," + acceleration[2] + "\n";
            content2 = (currentTime - startTime) + "," + "GYR" + "," + gyroscope[0] + "," + gyroscope[1] + "," + gyroscope[2] + "\n";
            content3 = (currentTime - startTime) + "," + "MAG" + "," + magnetometer[0] + "," +magnetometer[1] + "," +  magnetometer[2] + "\n";
            // as long the recording is ON
            for (int i = 0; i < 1; i++){
                try {
                    // create the file
                    out = openFileOutput(FILENAME, Context.MODE_APPEND);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                // write to the file
                    if (checkAcc.isChecked() && checkGyro.isChecked() && checkMagne.isChecked()) {
                        if (startFlag) {
                            handler10 = new Handler();
                            runnable10 = new Runnable() {
                                @Override
                                public void run() {
                                    handler10.postDelayed(this, 10);
                                    updateAccText();
                                    updateGyroText();
                                    try {
                                        out.write(content1.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    try {
                                        out.write(content2.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                            handler100 = new Handler();
                            runnable100 = new Runnable() {
                                @Override
                                public void run() {
                                    handler100.postDelayed(this, 100);
                                    updateMagneText();
                                    try {
                                        out.write(content3.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                        }
                    } else if (checkAcc.isChecked() && checkGyro.isChecked()) {
                        if (startFlag) {
                            runnable10 = new Runnable() {
                                @Override
                                public void run() {
                                    handler10.postDelayed(this, 10);
                                    updateAccText();
                                    updateGyroText();
                                    try {
                                        out.write(content1.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    try {
                                        out.write(content2.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                        }
                    } else if (checkAcc.isChecked() && checkMagne.isChecked()) {
                        if (startFlag) {
                            runnable10 = new Runnable() {
                                @Override
                                public void run() {
                                    handler10.postDelayed(this, 10);
                                    updateAccText();
                                    try {
                                        out.write(content1.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                            runnable100 = new Runnable() {
                                @Override
                                public void run() {
                                    handler100.postDelayed(this, 100);
                                    updateMagneText();
                                    try {
                                        out.write(content3.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                        }
                    } else if (checkGyro.isChecked() && checkMagne.isChecked()) {
                        if (startFlag) {
                            runnable10 = new Runnable() {
                                @Override
                                public void run() {
                                    handler10.postDelayed(this, 10);
                                    updateGyroText();
                                    try {
                                        out.write(content2.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                            runnable100 = new Runnable() {
                                @Override
                                public void run() {
                                    handler100.postDelayed(this, 100);
                                    updateMagneText();
                                    try {
                                        out.write(content3.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                        }
                    } else if (checkAcc.isChecked()) {
                        if (startFlag) {
                            runnable10 = new Runnable() {
                                @Override
                                public void run() {
                                    handler10.postDelayed(this, 10);
                                    updateAccText();
                                    try {
                                        out.write(content1.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                        }
                    } else if (checkGyro.isChecked()) {
                        if (startFlag) {
                            runnable10 = new Runnable() {
                                @Override
                                public void run() {
                                    handler10.postDelayed(this, 10);
                                    updateGyroText();
                                    try {
                                        out.write(content2.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                        }
                    } else if (checkMagne.isChecked()) {
                        if (startFlag) {
                            runnable100 = new Runnable() {
                                @Override
                                public void run() {
                                    handler100.postDelayed(this, 100);
                                    updateMagneText();
                                    try {
                                        out.write(content3.getBytes());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };
                        }
                    }
            } // for loop end
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    protected void updateAccText(){
        // Update the gyroscope data
        accX.setText(String.format("%.6f", acceleration[0]));
        accY.setText(String.format("%.6f", acceleration[1]));
        accZ.setText(String.format("%.6f", acceleration[2]));
    }

    protected void updateGyroText(){
        // Update the gyroscope data
        gyroX.setText(String.format("%.6f", gyroscope[0]));
        gyroY.setText(String.format("%.6f", gyroscope[1]));
        gyroZ.setText(String.format("%.6f", gyroscope[2]));
    }

    protected void updateMagneText(){
        // Update the gyroscope data
        magneX.setText(String.format("%.6f", magnetometer[0]));
        magneY.setText(String.format("%.6f", magnetometer[1]));
        magneZ.setText(String.format("%.6f", magnetometer[2]));
    }



}

当我按下记录(开始)按钮时,我希望创建一个 .csv 文件,该文件在更改之前保存每个已检查(选择)传感器的每个值,依此类推,直到用户按下保存(停止)按钮。

【问题讨论】:

    标签: java android-studio runnable sensors


    【解决方案1】:

    您创建了一个可运行的代码块,但从不执行它。当您创建一个可运行对象并希望将其设置为运行时调用它:

    new Thread(myRunnable).start();
    

    【讨论】:

      【解决方案2】:

      最后我成功了。 我每 5 毫秒保存一次 GYR 的值,每 100 毫秒保存一次 MAG,如果保存的值仍然与新值相等,则在它们不同时等待并写入。我认为这是一个很好的例子。

      我会把代码放在这里,也许它可以帮助其他人:

      Java:

      package localhost.dev.liverpoolsensorsfusion;
      
      import android.content.Context;
      import android.hardware.Sensor;
      import android.hardware.SensorEvent;
      import android.hardware.SensorEventListener;
      import android.hardware.SensorManager;
      import android.os.Handler;
      import android.os.SystemClock;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.CheckBox;
      import android.widget.Chronometer;
      import android.widget.EditText;
      import android.widget.TextView;
      import android.widget.Toast;
      
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      
      public class RecordDataActivity extends AppCompatActivity implements SensorEventListener {
      
          private SensorManager mSensorManager;
          private Sensor mAccelerometer;
          private Sensor mGyroscope;
          private Sensor mMagnetometer;
          CheckBox checkAcc;
          CheckBox checkGyro;
          CheckBox checkMagne;
          TextView accX;
          TextView accY;
          TextView accZ;
          TextView magneX;
          TextView magneY;
          TextView magneZ;
          TextView gyroX;
          TextView gyroY;
          TextView gyroZ;
          String FILENAME;
          String content1;
          String content2;
          String content3;
          FileOutputStream out;
          Button startButton;
          Button stopButton;
          boolean isFirstSet = true;
          boolean startFlag = false;
          EditText textData;
          private Chronometer chronometer;
          private boolean running_chronometer;
          private long currentTime;
          private long startTime;
      
          float[] acceleration = new float[3];
          float[] gyroscope = new float[3];
          float[] magnetometer = new float[3];
      
          float[] new_gyroscope = new float[3];
          float[] new_magnetometer = new float[3];
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_record_data);
              // define sensors
              mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
              mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
              mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
              mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
      
              // define text views
              accX = (TextView) findViewById(R.id.raw_value_acc_x);
              accY = (TextView) findViewById(R.id.raw_value_acc_y);
              accZ = (TextView) findViewById(R.id.raw_value_acc_z);
              magneX = (TextView) findViewById(R.id.raw_value_magne_x);
              magneY = (TextView) findViewById(R.id.raw_value_magne_y);
              magneZ = (TextView) findViewById(R.id.raw_value_magne_z);
              gyroX = (TextView) findViewById(R.id.raw_value_gyro_x);
              gyroY = (TextView) findViewById(R.id.raw_value_gyro_y);
              gyroZ = (TextView) findViewById(R.id.raw_value_gyro_z);
      
              // define checkboxes
              checkAcc=(CheckBox)findViewById(R.id.checkBox);
              checkGyro=(CheckBox)findViewById(R.id.checkBox2);
              checkMagne=(CheckBox)findViewById(R.id.checkBox3);
      
              // file name to be entered
              textData = (EditText) findViewById(R.id.edit_text);
              textData.setHint("Enter File Name here...");
      
              // define chronometer
              chronometer = findViewById(R.id.chronometer);
              chronometer.setFormat("Recording: %s");
              chronometer.setBase(SystemClock.elapsedRealtime());
      
              // define start button
              startButton = (Button) findViewById(R.id.button_record);
              startButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      // create file
                      FILENAME= textData.getText() + ".csv";
                      if(!checkAcc.isChecked() && !checkGyro.isChecked() && !checkMagne.isChecked()) {
                          Toast.makeText(getBaseContext(), "Please select at least one sensor!",
                                  Toast.LENGTH_LONG).show();
                      }else if(FILENAME.equals(".csv")){
                          Toast.makeText(getBaseContext(), "Please insert a valid name for the file to be created!",
                                  Toast.LENGTH_LONG).show();
                      }else {
                          // set the recording button ON
                          startFlag = true;
                          // make the chronometer run
                          if (!running_chronometer) {
                              chronometer.setBase(SystemClock.elapsedRealtime());
                              chronometer.start();
                              running_chronometer = true;
                              checkAcc.setClickable(false);
                              checkGyro.setClickable(false);
                              checkMagne.setClickable(false);
                          }
                          // add screen message to confirm that the app is recording
                          try{
                              textData.getText().clear();
                              Toast.makeText(getBaseContext(), "Start recording the data set", Toast.LENGTH_SHORT).show();
                          }catch(Exception e) {
                              Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                          }
                          // Turn off the record button
                          startButton.setClickable(false);
                      }
                  }
              }); // starts button ends here
      
              // define stop button
              stopButton=(Button)findViewById(R.id.button_stop);
              stopButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      // set the recording button OFF
                      // Stop writing on file
                      startFlag = false;
                      // if there is no file created state the following message
                      if(FILENAME==null || FILENAME.equals(".csv")){
                          Toast.makeText(getBaseContext(), "There is no recording taken in this moment!",
                                  Toast.LENGTH_LONG).show();
                      }else{
                          // stop the chronometer
                          chronometer.stop();
                          running_chronometer = false;
                          checkAcc.setClickable(true);
                          checkGyro.setClickable(true);
                          checkMagne.setClickable(true);
                          // add screen message to confirm that the app has saved the data set
                          try{
                              Toast.makeText(getBaseContext(), "Saved to " + getFilesDir() + "/" + FILENAME,
                                      Toast.LENGTH_LONG).show();
                          }catch(Exception e) {
                              Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                          }
                      }
                      // Turn back on the Record button
                      startButton.setClickable(true);
                  }
              });  // stop button ends here
          } // onCreate class ends here
      
          @Override
          public final void onAccuracyChanged(Sensor sensor, int accuracy) {
              // Do something here if sensor accuracy changes.
          }
      
          @Override
          public final void onSensorChanged(SensorEvent event) {
              if (startFlag) {
                  if (checkAcc.isChecked() && event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                      acceleration[0] = event.values[0];
                      acceleration[1] = event.values[1];
                      acceleration[2] = event.values[2];
                  }
      
                  if (checkGyro.isChecked() && event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                      gyroscope[0] = event.values[0];
                      gyroscope[1] = event.values[1];
                      gyroscope[2] = event.values[2];
                  }
      
                  if (checkMagne.isChecked() && event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                      magnetometer[0] = event.values[0];
                      magnetometer[1] = event.values[1];
                      magnetometer[2] = event.values[2];
                  }
                  // when recording is ON do this
                  // initialise the content that will be written in the file
                  if (isFirstSet) {
                      startTime = System.currentTimeMillis();
                      isFirstSet = false;
                  }
                  currentTime = System.currentTimeMillis();
      
                  content1 = (currentTime-startTime) + "," + "ACC" + "," + acceleration[0] + "," + acceleration[1] + "," + acceleration[2] + "\n";
                  content2 = (currentTime-startTime) + "," + "GYR" + "," + gyroscope[0] + "," + gyroscope[1] + "," + gyroscope[2] + "\n";
                  content3 = (currentTime-startTime) + "," + "MAG" + "," + magnetometer[0] + "," +magnetometer[1] + "," +  magnetometer[2] + "\n";
                  // as long the recording is ON
                  for (int i = 0; i < 1; i++){
                      try {
                          // create the file
                          out = openFileOutput(FILENAME, Context.MODE_APPEND);
                      } catch (FileNotFoundException e) {
                          e.printStackTrace();
                      }
                      // write to the file
                      if (checkAcc.isChecked() && checkGyro.isChecked() && checkMagne.isChecked() && startFlag) {
                          updateAccText();
                          writeAcc();
                          updateGyroText();
                          HandleGyr();
                          if(new_gyroscope[0]==gyroscope[0] && new_gyroscope[1]== gyroscope[1] && new_gyroscope[2]==gyroscope[2]){
                              writeGyr();
                          }
                          updateMagneText();
                          HandleMag();
                          if(new_magnetometer[0]==magnetometer[0] && new_magnetometer[1] == magnetometer[1] && new_magnetometer[1] == magnetometer[1]){
                              writeMag();
                          }
                      } else if (checkAcc.isChecked() && checkGyro.isChecked() && startFlag) {
                          updateAccText();
                          updateGyroText();
                          writeAcc();
                          HandleGyr();
                          if(new_gyroscope[0]==gyroscope[0] && new_gyroscope[1]== gyroscope[1] && new_gyroscope[2]==gyroscope[2]){
                              writeGyr();
                          }
                      } else if (checkAcc.isChecked() && checkMagne.isChecked() && startFlag) {
                          updateAccText();
                          updateMagneText();
                          writeAcc();
                          HandleMag();
                          if(new_magnetometer[0]==magnetometer[0] && new_magnetometer[1] == magnetometer[1] && new_magnetometer[1] == magnetometer[1]){
                              writeMag();
                          }
                      } else if (checkGyro.isChecked() && checkMagne.isChecked() && startFlag) {
                          updateGyroText();
                          updateMagneText();
                          HandleGyr();
                          HandleMag();
                          if(new_gyroscope[0]==gyroscope[0] && new_gyroscope[1]== gyroscope[1] && new_gyroscope[2]==gyroscope[2]){
                              writeGyr();
                          }
                          if(new_magnetometer[0]==magnetometer[0] && new_magnetometer[1] == magnetometer[1] && new_magnetometer[1] == magnetometer[1]){
                              writeMag();
                          }
                      } else if (checkAcc.isChecked() && startFlag) {
                          updateAccText();
                          writeAcc();
                      } else if (checkGyro.isChecked() && startFlag) {
                          updateGyroText();
                          writeGyr();
                      } else if (checkMagne.isChecked()) {
                          updateAccText();
                          writeAcc();
                      }
                  } // for loop end
              }
      
          }
          @Override
          protected void onResume() {
              super.onResume();
              mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
              mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
              mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
          }
          @Override
          protected void onPause() {
              super.onPause();
              mSensorManager.unregisterListener(this);
          }
          protected void updateAccText(){
              // Update the gyroscope data
              accX.setText(String.format("%.6f", acceleration[0]));
              accY.setText(String.format("%.6f", acceleration[1]));
              accZ.setText(String.format("%.6f", acceleration[2]));
          }
          protected void updateGyroText(){
              // Update the gyroscope data
              gyroX.setText(String.format("%.6f", gyroscope[0]));
              gyroY.setText(String.format("%.6f", gyroscope[1]));
              gyroZ.setText(String.format("%.6f", gyroscope[2]));
          }
          protected void updateMagneText(){
              // Update the gyroscope data
              magneX.setText(String.format("%.6f", magnetometer[0]));
              magneY.setText(String.format("%.6f", magnetometer[1]));
              magneZ.setText(String.format("%.6f", magnetometer[2]));
          }
          // Adjust sampling rate for Gyroscope
          public void HandleGyr(){
              Handler handlerGyroscope = new Handler();
              handlerGyroscope.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      new_gyroscope[0]=gyroscope[0];
                      new_gyroscope[1]=gyroscope[1];
                      new_gyroscope[2]=gyroscope[2];
                  }
              }, 5);
          }
          // Adjust sampling rate for Magnetometer
          public void HandleMag(){
              Handler handlerMagnetometer = new Handler();
              handlerMagnetometer.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      new_magnetometer[0]=magnetometer[0];
                      new_magnetometer[1]=magnetometer[1];
                      new_magnetometer[2]=magnetometer[2];
                  }
              }, 100);
          }
      
          public void writeAcc(){
              try {
                  out.write(content1.getBytes());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      
          public void writeGyr(){
              try {
                  out.write(content2.getBytes());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      
          public void writeMag(){
              try {
                  out.write(content3.getBytes());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      

      XML:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".RecordDataActivity">
      
          <TextView
              android:id="@+id/raw_label_acc_x"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentStart="true"
              android:layout_below="@+id/checkBox"
              android:layout_marginStart="133dp"
              android:paddingRight="20dp"
              android:text="X:"
              android:textColor="@color/graphX" />
      
          <TextView
              android:id="@+id/raw_label_acc_y"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="133dp"
              android:layout_below="@+id/raw_label_acc_x"
              android:paddingRight="20dp"
              android:text="Y:"
              android:textColor="@color/graphY" />
      
          <TextView
              android:id="@+id/raw_label_acc_z"
              android:layout_width="wrap_content"
              android:layout_marginStart="133dp"
              android:layout_height="wrap_content"
              android:layout_below="@+id/raw_label_acc_y"
              android:paddingRight="20dp"
              android:text="Z:"
              android:textColor="@color/graphZ" />
      
          <TextView
              android:id="@+id/raw_value_acc_x"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_acc_x"
              android:layout_alignBottom="@+id/raw_label_acc_x"
              android:layout_toEndOf="@+id/raw_label_acc_x"
              android:textColor="@color/graphX" />
      
          <TextView
              android:id="@+id/raw_value_acc_y"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_acc_y"
              android:layout_alignBottom="@+id/raw_label_acc_y"
              android:layout_toEndOf="@+id/raw_label_acc_y"
              android:textColor="@color/graphY" />
      
          <TextView
              android:id="@+id/raw_value_acc_z"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_acc_z"
              android:layout_alignBottom="@+id/raw_label_acc_z"
              android:layout_toEndOf="@+id/raw_label_acc_z"
              android:textColor="@color/graphZ" />
      
      
          <TextView
              android:id="@+id/raw_label_gyro_x"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentStart="true"
              android:layout_below="@+id/checkBox2"
              android:layout_marginStart="133dp"
              android:paddingRight="20dp"
              android:text="X:"
              android:textColor="@color/graphX" />
      
          <TextView
              android:id="@+id/raw_label_gyro_y"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="133dp"
              android:layout_below="@+id/raw_label_gyro_x"
              android:paddingRight="20dp"
              android:text="Y:"
              android:textColor="@color/graphY" />
      
          <TextView
              android:id="@+id/raw_label_gyro_z"
              android:layout_width="wrap_content"
              android:layout_marginStart="133dp"
              android:layout_height="wrap_content"
              android:layout_below="@+id/raw_label_gyro_y"
              android:paddingRight="20dp"
              android:text="Z:"
              android:textColor="@color/graphZ" />
      
          <TextView
              android:id="@+id/raw_value_gyro_x"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_gyro_x"
              android:layout_alignBottom="@+id/raw_label_gyro_x"
              android:layout_toEndOf="@+id/raw_label_gyro_x"
              android:textColor="@color/graphX" />
      
          <TextView
              android:id="@+id/raw_value_gyro_y"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_gyro_y"
              android:layout_alignBottom="@+id/raw_label_gyro_y"
              android:layout_toEndOf="@+id/raw_label_gyro_y"
              android:textColor="@color/graphY" />
      
          <TextView
              android:id="@+id/raw_value_gyro_z"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_gyro_z"
              android:layout_alignBottom="@+id/raw_label_gyro_z"
              android:layout_toEndOf="@+id/raw_label_gyro_z"
              android:textColor="@color/graphZ" />
      
          <TextView
              android:id="@+id/raw_label_magne_x"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentStart="true"
              android:layout_below="@+id/checkBox3"
              android:layout_marginStart="133dp"
              android:paddingRight="20dp"
              android:text="X:"
              android:textColor="@color/graphX" />
      
          <TextView
              android:id="@+id/raw_label_magne_y"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="133dp"
              android:layout_below="@+id/raw_label_magne_x"
              android:paddingRight="20dp"
              android:text="Y:"
              android:textColor="@color/graphY" />
      
          <TextView
              android:id="@+id/raw_label_magne_z"
              android:layout_width="wrap_content"
              android:layout_marginStart="133dp"
              android:layout_height="wrap_content"
              android:layout_below="@+id/raw_label_magne_y"
              android:paddingRight="20dp"
              android:text="Z:"
              android:textColor="@color/graphZ" />
      
          <TextView
              android:id="@+id/raw_value_magne_x"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_magne_x"
              android:layout_alignBottom="@+id/raw_label_magne_x"
              android:layout_toEndOf="@+id/raw_label_magne_x"
              android:textColor="@color/graphX" />
      
          <TextView
              android:id="@+id/raw_value_magne_y"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_magne_y"
              android:layout_alignBottom="@+id/raw_label_magne_y"
              android:layout_toEndOf="@+id/raw_label_magne_y"
              android:textColor="@color/graphY" />
      
          <TextView
              android:id="@+id/raw_value_magne_z"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/raw_label_magne_z"
              android:layout_alignBottom="@+id/raw_label_magne_z"
              android:layout_toEndOf="@+id/raw_label_magne_z"
              android:textColor="@color/graphZ" />
      
      
          <Chronometer
              android:id="@+id/chronometer"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_centerHorizontal="true"
              android:layout_marginBottom="193dp"
              android:textSize="22sp" />
      
          <EditText
              android:id="@+id/edit_text"
              android:layout_width="match_parent"
              android:layout_height="58dp"
              android:layout_alignParentBottom="true"
              android:layout_alignParentStart="true"
              android:background="@drawable/rounded_edittext"
              android:inputType="" />
      
      
          <Button
              android:id="@+id/button_record"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_centerHorizontal="true"
              android:layout_marginBottom="126dp"
              android:background="@drawable/button_quit"
              android:shadowColor="#8A8A8A"
              android:shadowDx="0"
              android:shadowDy="1"
              android:text="@string/record"
              android:textColor="#FFF1D6"
              android:textSize="18sp"
              tools:ignore="OnClick"
              tools:text="Record" />
      
          <Button
              android:id="@+id/button_stop"
              android:layout_width="181dp"
              android:layout_height="38dp"
              android:layout_alignParentBottom="true"
              android:layout_centerHorizontal="true"
              android:layout_marginBottom="79dp"
              android:background="@drawable/button_bulb_led"
              android:shadowColor="#8A8A8A"
              android:shadowDx="0"
              android:shadowDy="1"
              android:text="@string/save"
              android:textColor="#FFF1D6"
              android:textSize="18sp"
              tools:ignore="OnClick"
              tools:text="Save" />
      
          <CheckBox
              android:id="@+id/checkBox"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:text="@string/accelerometer"
              android:textSize="20sp" />
      
          <CheckBox
              android:id="@+id/checkBox2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignEnd="@+id/button_record"
              android:layout_below="@+id/raw_label_acc_z"
              android:text="@string/gyroscope"
              android:textSize="20sp" />
      
          <CheckBox
              android:id="@+id/checkBox3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/raw_label_gyro_z"
              android:layout_centerHorizontal="true"
              android:text="@string/magnetometer"
              android:textSize="20sp" />
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        • 2021-02-16
        • 2015-01-23
        • 1970-01-01
        • 1970-01-01
        • 2015-06-26
        • 2021-05-05
        相关资源
        最近更新 更多