Using SensorManager to calculate acceleration and check if it is still
//package com.hairy.nipples.util;
import android.hardware.SensorManager;
class AccelerationUtil {
public static double calculateAcceleration(float[] values) {
double acceleration = Math.sqrt(Math.pow(values[0], 2)
+ Math.pow(values[1], 2) + Math.pow(values[2], 2));
return acceleration;
}
public static boolean isStandingStill(float[] accelerationValues) {
double acceleration = calculateAcceleration(accelerationValues);
// If acceleration doesn't differ from earth's gravity more than 10%, then
// it's safe to assume that phone is standing still
if (acceleration > SensorManager.GRAVITY_EARTH * 0.9
&& acceleration < SensorManager.GRAVITY_EARTH * 1.1) {
return true;
}
return false;
}
}
Related examples in the same category