# Class MotionSensor

java.lang.Object
com.codename1.sensors.MotionSensor

public class MotionSensor extends Object

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.

- ## Method Summary

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

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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`: 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`: 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.