Android - 将传感器数据保存到文件

时间:2017-03-02 22:50:26

标签: java android file accelerometer file-writing

我正在尝试将获得的加速度计数据保存到手机上的文件中。我每秒都获得数据,我想将其保存到文件中。这是我的尝试:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start = (Button)findViewById(R.id.button_start);
    stop = (Button)findViewById(R.id.button_stop) ;


    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    if (sensors.size() > 0) {
        for (Sensor sensor : sensors) {
            switch (sensor.getType()) {
                case Sensor.TYPE_LINEAR_ACCELERATION:
                    if (mAccelerometerSensor == null) mAccelerometerSensor = sensor;
                    break;

                default:
                    break;
            }
        }
    }
    context= this;

    mXAccValueText = (TextView)findViewById(R.id.value_x);
    mYAccValueText = (TextView)findViewById(R.id.value_y);
    mZAccValueText = (TextView)findViewById(R.id.value_z);


    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                writer = new FileWriter("data.txt",true);
            } catch (IOException e) {
                e.printStackTrace();
            }

            time = System.currentTimeMillis();
            mSensorManager.registerListener(MainActivity.this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);

        }

        });
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            mSensorManager.unregisterListener(MainActivity.this);
        }
    });
}

@Override
protected void onPause() {
    super.onPause();

}

@Override
protected void onResume() {
    super.onResume();

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}


public void onSensorChanged(SensorEvent event) {
    long currentTime;
    switch (event.sensor.getType()) {

        case Sensor.TYPE_LINEAR_ACCELERATION:
            currentTime = System.currentTimeMillis();
            if (currentTime - accelerometerTime > 1000) {

                float x = event.values[0];
                float y = event.values[1];
                float z = event.values[2];
                try {
                    writer.write(x+","+y+","+z+"\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }

                accelerometerTime = currentTime;
            }
            break;
    }
}

但是,我只是不断获得例外。我是Android新手,有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

 if (currentTime - accelerometerTime > 1000) 

你之前设置了accelerometerTimer吗?在给出的代码中,您只能在之后设置它?