Accelerometer Android App (with Source code)

Download APK Download Source Code Contact Us

Using gestures often feels more natural than interacting with a user interface through mouse and keyboard. This is especially true for touch devices, such as smartphones and tablets. I find that using gestures can bring an Android application to life, making it more interesting and exciting for the user.

In this tutorial, we’ll use a accelerometer that you find in almost all android mobile device. We’ll use the accelerometer to identify the horizontal and vertical applied acceleration. And show the reading on x,y and z axis.

An acceleration sensor measures the acceleration applied to the device, including the force of gravity. The following code shows you how to get an instance of the default acceleration sensor:

private SensorManager mSensorManager;
private Sensor mAccelerometer;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
You can access the sensor via the sensorManager.getDefaultSensor() method, which takes the sensor type and the delay defined as constants on SensorManager as parameters.
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}
protected void onResume() {
    super.onResume();
    senSensorManager.registerListener(this, mAccelerometer, mSensorManager.SENSOR_DELAY_NORMAL);
}

To avoid the unnecessary usage of battery power, you can register your listener in the onResume() method and de-register it in the onPause() method.

In order to receive notifications from the SensorManager when sensor values have changed.You have to implement SensorEventListener. Which has two public methods.

@Override
public void onSensorChanged(SensorEvent event) {
}@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

We can now start to focus on the meat of the application. It will require a bit of math to figure out direction of acceleration takes place. Most of the logic will go into theonSensorChanged method. We start by declaring a few variables in our class. Take a look at the code snippet below.

private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private final float THRESHOLD = (float) 2.0;

To get the values of each axis, we ask the sensor event for its values as shown below. The event’s values attribute is an array of floats.

float x = event.values[0];
float y = event.values[1];
float z = event.values[2];

To keep the record of the initialisation of the accelerometer.We first hold the value of the previous x,y and then keep a flag to check whether its first time or not.

private boolean mInitialized;
@Override
	public void onSensorChanged(SensorEvent event) {
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		if (!mInitialized) {
			mLastX = x;
			mLastY = y;
			mLastZ = z;			
			mInitialized = true;
		} else {
			float deltaX = Math.abs(mLastX - x);
			float deltaY = Math.abs(mLastY - y);
			float deltaZ = Math.abs(mLastZ - z);
			if (deltaX < THRESHOLD) deltaX = (float)0.0;
			if (deltaY < THRESHOLD) deltaY = (float)0.0;
			if (deltaZ < THRESHOLD) deltaZ = (float)0.0;
			mLastX = x;
			mLastY = y;
			mLastZ = z;
			iv.setVisibility(View.VISIBLE);
			if (deltaX > deltaY) {
				//Horizaontal acceleration
			} else if (deltaY > deltaX) {
				//vertical acceleration
			} else {
				//No acceleration	
			}
		}

Download APK Download Source Code Contact Us

Related Posts