public class MotionSensor

  1. Object
  2. MotionSensor

Represents a single motion sensor (for example the accelerometer or the gyroscope). Obtain an instance from MotionSensorManager.getSensor(int) and start receiving readings by registering a MotionSensorListener:

MotionSensor accel = MotionSensorManager.getInstance().getSensor(MotionSensorManager.TYPE_ACCELEROMETER);
if (accel != null) {
    accel.addListener(new MotionSensorListener() {
        public void motionReceived(MotionEvent evt) {
            label.setText("x=" + evt.getX() + " y=" + evt.getY() + " z=" + evt.getZ());
        }
    });
}

Sampling is reference counted: the underlying hardware sensor is only active while at least one listener is registered, so always remove your listener (typically when the form is no longer showing) to conserve battery.

Methods

public int getType()The type of this sensor, one of the MotionSensorManager.TYPE_* constants.
public boolean isSupported()Whether this sensor is available on the current device.
public void addListener(MotionSensorListener l)Registers a listener that will receive readings on the EDT.
public void removeListener(MotionSensorListener l)Removes a previously registered listener.
public float getX()The most recent value of the x axis, or 0 if no reading has arrived yet.
public float getY()The most recent value of the y axis, or 0 if no reading has arrived yet.
public float getZ()The most recent value of the z axis, or 0 if no reading has arrived yet.
public boolean hasReading()Whether at least one reading has been received from this sensor.
public long getTimestamp()The timestamp of the most recent reading in milliseconds.

Inherited methods

Method details

getType

public int getType()
The type of this sensor, one of the MotionSensorManager.TYPE_* constants.

isSupported

public boolean isSupported()
Whether this sensor is available on the current device. A sensor with no hardware backing can still be obtained but will never deliver readings.

addListener

public void addListener(MotionSensorListener l)
Registers a listener that will receive readings on the EDT. Registering the first listener starts the underlying hardware sensor.

Parameters

l MotionSensorListener
the listener to add, ignored if null or already registered

removeListener

public void removeListener(MotionSensorListener l)
Removes a previously registered listener. Removing the last listener stops the underlying hardware sensor.

Parameters

l MotionSensorListener
the listener to remove

getX

public float getX()
The most recent value of the x axis, or 0 if no reading has arrived yet.

getY

public float getY()
The most recent value of the y axis, or 0 if no reading has arrived yet.

getZ

public float getZ()
The most recent value of the z axis, or 0 if no reading has arrived yet.

hasReading

public boolean hasReading()
Whether at least one reading has been received from this sensor.

getTimestamp

public long getTimestamp()
The timestamp of the most recent reading in milliseconds.