This article goes over the basics of using the Android motion sensing APIs. For simple applications you might want to use motion sensing to turn features like sounds or settings on and off. For more complex applications like games you can control complex movements of on screen objects. Controlling movements requires not only the motion sensing APIs but also a good understanding of how the numbers returned by the APIs
change and of the physics related to the objects you are controlling. These are complex topics which we will not be covering in this article.
The Example Application
The application shows an image whenever it senses that the device has moved. There is a button that can be used to enable/disable the motion sensor. (The image used in the app is the Android application icon that is included with all new projects created in Eclipse.)
The API
Our application uses the Accelerometer. The API functions through a listener. When motion is detected the device calls the listener and passes data representing that motion. The data is in the form of a three element array. The three elements correspond to acceleration along the three axis X, Y and Z. Your application has to implement the listener. The listener is where you put code that you want to respond when motion occurs. In our app we check how much the values have changed since the last call to the listener. If they have changed only a little we ignore it. If they have changed more than a little we conclude that the device has moved and we do something (ie. show an image).
The code in our listener looks like this:
/* WITH COMMENTS */
// check each of the three Axis
for (int i = 0; i < event.values.length; i++) {
// calc diff with value recorded on last pass
float diff = Math.abs(event.values[i] - last_values[i]);
// ignore small differences
if(diff > (last_values[i]*SENSITIVITY)){
// show the image
ifImage.setVisibility(View.VISIBLE);
// image will be cleared after set amount of time
last_time_moved = System.currentTimeMillis();
}
}
// save current values to last_values
/* WITHOUT COMMENTS */
for (int i = 0; i < event.values.length; i++) {
float diff = Math.abs(event.values[i] - last_values[i]);
if(diff > (last_values[i]*SENSITIVITY)){
ifImage.setVisibility(View.VISIBLE);
last_time_moved = System.currentTimeMillis();
}
}
Getting Android to Listen
Sensor Manager sensor_manager = (SensorManager)getSystemService(SENSOR_SERVICE);
// register sensor_manager.registerListener( (SensorEventListener)MPMotionSensorActivity.this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
sensor = sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
Where to Listen
Only Listen When Necessary
sensor_manager.unregisterListener( (SensorEventListener)this, sensor);
Thats all there is to using sensors in Android. You can download the Activity and Layout files from here > AndroidMotionApp <
MarkP
References
Android Developer’s Reference on Sensors
http://developer.android.com/guide/topics/sensors/sensors_motion.html
Discussion of Event Data for Various Sensors
http://developer.android.com/reference/android/hardware/SensorEvent.html



