How to detect that phone was moved f.e. 10 meters in 2s using Accelerometer? I need to track when motion starts to switch on GPS.
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
float accel = FloatMath.sqrt(x * x + y * y + z * z);
g = g * 0.9f + accel * 0.1f;
if ((System.currentTimeMillis() - mSensorTimestamp) < 2000) return;
//What I have to do here?
mSensorManager.unregisterListener(this);
}
I think you are mistaken, the accelerometer does not work this way, like a position tracker. Check The motion docs on d.android.com to see what I mean.
From the docs:
Accelerometers use the standard sensor coordinate system. In practice, this means that the following conditions apply when a device is laying flat on a table in its natural orientation:
If you push the device on the left side (so it moves to the right), the x acceleration value is positive. If you push the device on the bottom (so it moves away from you), the y acceleration value is positive. If you push the device toward the sky with an acceleration of A m/s2, the z acceleration value is equal to A + 9.81, which corresponds to the acceleration of the device (+A m/s2) minus the force of gravity (-9.81 m/s2). The stationary device will have an acceleration value of +9.81, which corresponds to the acceleration of the device (0 m/s2 minus the force of gravity, which is -9.81 m/s2).
So, I'd recommend you to use the Location Service directly !
Hope it helps!