I am a beginner at android programming and I have come up with some progress on getting the sensor data (Source code below). Can someone help me with detecting how fast my Android device is moving using these information? For example if I am moving the device fast, it will give me like 10, and if I am moving it slowly, I will get like 1. Aside from accelerometer, all other sensors are also OK for me.
package course.examples.Sensors.ShowValues;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class SensorRawAccelerometerActivity extends Activity implements
SensorEventListener {
private static final int UPDATE_THRESHOLD = 500;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private TextView mXValueView, mYValueView, mZValueView;
private long mLastUpdate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mXValueView = (TextView) findViewById(R.id.x_value_view);
mYValueView = (TextView) findViewById(R.id.y_value_view);
mZValueView = (TextView) findViewById(R.id.z_value_view);
// Get reference to SensorManager
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Get reference to Accelerometer
if (null == (mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)))
finish();
}
// Register listener
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_UI);
mLastUpdate = System.currentTimeMillis();
}
// Unregister listener
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
// Process new reading
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
long actualTime = System.currentTimeMillis();
if (actualTime - mLastUpdate > UPDATE_THRESHOLD) {
mLastUpdate = actualTime;
float x = event.values[0], y = event.values[1], z = event.values[2];
mXValueView.setText(String.valueOf(x));
mYValueView.setText(String.valueOf(y));
mZValueView.setText(String.valueOf(z));
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// N/A
}
}
Accelerometers measure change in speed (acceleration), not speed itself. If you are looking for short distances you are out of luck. Long distances you can poll this data from the GPS which gives you a heading and average velocity.