Is the Phone Moving or Not?

It is really amazing to see how mobile devices evolved over the past years. I mean, I'm 25 now, and I remember my first phone, a Nokia 3210. I was that happy to get it, and you couldn't just phone, you could also send tiny text messages ;). Nowadays children would probably laugh seing such a phone, given the new smartphones which currently hit the market.

Obviously, as a developer, what's most interesting about all that stuff is to code for these devices. It really gives you endless new opportunities to write amazing apps which you can carry around in your pocket :). Not only is the computing power increasing with every new devices which comes out, but also the amount of sensors such a device has build in is so cool: Bluetooth (obviously) with which you can really do nice stuff I'd never have imagined if not writing my current MSc thesis about it, accelerator sensors, orientation sensors etc...

I mean this is mostly geeky stuff, but how cool is it to write a small app which "knows" whether the phone is moving or laying somewhere on the table? In Android this turns out to be pretty simple..
Not moving
In movement
Android has a SensorManager which gives you access to the accelerometer sensor. Having that, you can read the according device's x, y and z coordinates.
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class TestActivity extends Activity implements SensorEventListener {
private SensorManager mgr;
private Sensor sensorAccelerometer;
private long lastUpdate;
private float lastX = 0.5f;
private float lastY = 0.5f;
private float lastZ = 0.9f;

protected void onCreate(Bundle savedInstanceState) {

mgr = (SensorManager)getSystemService(SENSOR_SERVICE);
sensorAccelerometer = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

protected void onResume() {
mgr.registerListener(this, sensorAccelerometer, SensorManager.SENSOR_DELAY_UI);

protected void onPause() {

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

public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
long currTime = System.currentTimeMillis();

if((currTime - lastUpdate) > 1000){ //just scan every second
lastUpdate = currTime;

float xAxis = event.values[SensorManager.DATA_X];
float yAxis = event.values[SensorManager.DATA_Y];
float zAxis = event.values[SensorManager.DATA_Z];

String comment = inMovement(xAxis, yAxis, zAxis) ? "not moving" : "in movement";

publishValues(xAxis, yAxis, zAxis, comment);

lastX = xAxis;
lastY = yAxis;
lastZ = zAxis;

private boolean inMovement(float xAxis, float yAxis, float zAxis) {
final float tolerance = 0.5f;

if(Math.abs(xAxis - lastX) <= tolerance && Math.abs(yAxis - lastY) <= tolerance && Math.abs(zAxis - lastZ)<=tolerance){
return true;
return false;

private void publishValues(float x, float y, float z, String comment) {
TextView mainText =(TextView)findViewById(;

mainText.setText("X: " + x + " - Y: " + y + " - Z: " + z + "\nComment: " + comment);
Don't look at clean code here, alright ;)? It's just a prototype. I'm not going to describe it in detail, it's quite simple and you'll find it out.

By the way, the SensorManager is also documented on But pay attention at the code there. There are some errors in there like the "," after the class isn't valid Java syntax
public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
and retrieving the SensorManager in the Activity's constructor won't work because it is yet available at that time. You better create it in the onCreate(...) ;).



Your ad here?