Android – Accelerometer Basic Usage
Do you want to access the accelerometer (or some other sensor) in your device and use its data in your application? Here is a way of doing that. Let’s see the basic form of an application that monitors changes to the accelerometer sensor:
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class TestMain extends Activity implements SensorEventListener{
private TextView mainText; // here we display accelerometer info
private SensorManager mySensorManager; // used to acquire sensor access
private Sensor accSensor; // accelerometer sensor object
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainText = (TextView)findViewById(R.id.main_text);
// Set SensorManager and acquire a reference to accelerometer sensor
mySensorManager = (SensorManager)getSystemService(
Context.SENSOR_SERVICE);
accSensor = mySensorManager.getSensorList(
Sensor.TYPE_ACCELEROMETER).get(0);
// it may be a good idea to first check the returned Sensor list
// size to be sure we actually did get an accelerometer reference
// ...
}
// register current SensorEventListener
@Override
protected void onResume()
{
super.onResume();
mySensorManager.registerListener(this, accSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
// unregister current SensorEventListener
@Override
protected void onStop()
{
mySensorManager.unregisterListener(this);
super.onStop();
}
// receives accuracy changes form sensor
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d("TestingArea","onAccuracyChanged: " + sensor +
" has accuracy: " + accuracy);
}
// receives sensor changes
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mainText.setText("acc x = " + event.values[0] +
"\nacc y = " + event.values[1] +
"\nacc z = " + event.values[2]);
}
// ...
}
- We need a SensorManager object to access sensors (we instantiated it in the onCreate() method by using Context method getSystemService(Context.SENSOR_SERVICE)).
- We use getSensorList() method on the SensorManager object to acquire the sensors we want – we use as parameter TYPE_ACCELEROMETER to access accelerometer sensor (for others take a look at Sensor documentation).
- We need a class implementing SensorEventListener to be able to receive accelerometer changes – we use the main Activity as the Listener in this example, other options may be more appropriate in other situations. Try not to use SensorListener interface as it is deprecated!
- The SensorEventListener must implement two methods – onSensorChanged() and onAccuracyChanged() – and that’s where we’ll receive the accelerometer values.
- We need to register the Listener with the SensorManager to start receiving accelerometer info. A good place to do that is onResume() method as the Activity could be restarted by orientation change or other events . Also unregistering the Listener when it’s not needed is recommended – a good place to do that is onStop() method.
- That’s all, we now receive SensorEvents and we can easily get the three axis accelerometer values by using event.values[x] in our onSensorChanged() method.
A full tutorial with in depth examples of Sensors will follow hopefully soon. Stay tuned.
size to be

Facebook
Twitter
Hi, I have tried re-using the code above. However it results in the following errors:
R can not be resolved to a variable: in this line of the code:
setContentView(R.layout.main);
Secondly, another error in the following line: with “id can not be resolved”.
Please share your opinion on the matter as I am terribly confused by this problem.
With best regards,
Umair