Sensor Manager

In this lecture, we discuss Android's sensors and accessing them via the SensorManager.

Nexus-4 as a number of interesting sensors beyond the camera and the microphone, specifically:

In this lecture we will focus mainly on the accelerometer. The Sensor Manager provides access to the 3-axis accelerometer sensor. We use the signals (x, y, and z axis) to infer activity in MyRuns5; specifically, we build an accelerometer pipeline that extracts "features" from the signals and uses these features to infer the activity of the user. More on that in MyRuns5 lab.

We first provide a demo of the accelerometer signals and the Sensor Manager. We will also show you how to access some of the other sensors such as light, barometer.

Note, these notes are incomplete.

What this lecture will teach you

Demo projects

The demo code used in this lecture include:


Some excellent references.

ShakeSensor Demo

This simple demo displays the x, y and z axis accelerometer readings in a continuous fashion. If the phone is shaken the background color of the text changes and a toast is displayed, as shown in the example below.

Set up Sensor Manager

We first get a sensor service and sensor manager before we can access the accelerometer data.

Set the Sensor Data Type of Interest

We set the sensor manager to get accelerometer data -- Sensor.TYPE_ACCELEROMETER. But we could have asked for any sensor data that the phone produces -- light, proximity, etc. See the list of sensor types here.

Phone Coordinate-system orientation

The figure below shows the x, y and z axis of the accelerometer sensor with respect to the phone's orientation.

See developers notes on the definition of the coordination and the SensorEvent object definition which is based to the onSensorChanged() callback when new data is available.

The X axis is horizontal and points to the right, the Y axis is vertical and points up and the Z axis points towards the outside of the front face of the screen. In this system, coordinates behind the screen have negative Z values.

public class SensorTestActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;
    private boolean color = false;
    private View view;
    private long lastUpdate;
    TextView textx, texty, textz;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // get textviews
        textx = (TextView) findViewById(;
        texty = (TextView) findViewById(;
        textz = (TextView) findViewById(;

        view = findViewById(;

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        lastUpdate = System.currentTimeMillis();

    public void onSensorChanged (SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {


Display accelerometer readings

The two helper functions display the accelerometer and check to see if the phone has been shaken using a simple test -- if so change the background color of the text.

    private void displayAccelerometer(SensorEvent event) {

        // Many sensors return 3 values, one for each axis.
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        // display values using TextView
        textx.setText("X axis" + "\t\t" + x);
        texty.setText("Y axis" + "\t\t" + y);
        textz.setText("Z axis" + "\t\t" + z);


    private void checkShake(SensorEvent event) {

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

        float accelationSquareRoot = (x * x + y * y + z * z)
             / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long actualTime = System.currentTimeMillis();
        if (accelationSquareRoot >= 2) //
            if (actualTime - lastUpdate < 200) {
            lastUpdate = actualTime;
            Toast.makeText(this, "Don't shake me!", Toast.LENGTH_SHORT).show();
            if (color) {

            } else {
            color = !color;

    public void onAccuracyChanged(Sensor sensor, int accuracy) {


    protected void onResume() {
        // register this class as a listener for the orientation and
        // accelerometer sensors

    protected void onPause() {
        // unregister listener