如何将加速度计数据保存到android中的日志文件中?

时间:2014-09-16 05:14:56

标签: android logging accelerometer sensor

我创建了一个读取加速度计数据的应用程序。它显示了x轴,y轴和z轴的不同值。我想将这些数据保存到日志文件中,以便我可以检索它们并分析数据。我所获得的价值也在快速变化。请帮我把这些值保存在SD卡或日志文件中,以及如何减慢变化值

public class MainActivity extends Activity implements SensorEventListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private float NOISE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}



@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    TextView tvX= (TextView)findViewById(R.id.x_axis);
    TextView tvY= (TextView)findViewById(R.id.y_axis);
    TextView tvZ= (TextView)findViewById(R.id.z_axis);
    ImageView iv = (ImageView)findViewById(R.id.image);
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    if (!mInitialized) {
    mLastX = x;
    mLastY = y;
    mLastZ = z;
    tvX.setText("0.0");
    tvY.setText("0.0");
    tvZ.setText("0.0");
    mInitialized = true;
    } else {
    float deltaX = Math.abs(mLastX - x);
    float deltaY = Math.abs(mLastY - y);
    float deltaZ = Math.abs(mLastZ - z);
    if (deltaX < NOISE) deltaX = (float)0.0;
    if (deltaY < NOISE) deltaY = (float)0.0;
    if (deltaZ < NOISE) deltaZ = (float)0.0;
    mLastX = x;
    mLastY = y;
    mLastZ = z;
    tvX.setText(Float.toString(deltaX));
    tvY.setText(Float.toString(deltaY));
    tvZ.setText(Float.toString(deltaZ));
    iv.setVisibility(View.VISIBLE);
    if (deltaX > deltaY) {
    iv.setImageResource(R.drawable.horizontal);
    } else if (deltaY > deltaX) {
    iv.setImageResource(R.drawable.vertical);
    } else {
    iv.setVisibility(View.INVISIBLE);
    }
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

} 

1 个答案:

答案 0 :(得分:0)

你不熟悉

Log.d("String tag","String value");

Log用于记录logcat上的响应。你在用什么编辑器?这是日食吗?