public class

SensorManager

extends Object
java.lang.Object
   ↳ com.neomades.io.sensor.SensorManager

Class Overview

A SensorManager lets you access the device's sensors. Get an instance of this class by calling getDefault(), this class can be instantiated only once. Then, you can get any sensor you want by calling the appropriate getter. Always make sure to disable sensors you don't need, especially when your application is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off.

 public final class AccelerometerScreen extends Screen implements SensorListener {
 
 	AccelerometerSensor accelerometer;
 	SensorManager manager;
 
 	protected void onCreate() {
 
 		manager = SensorManager.getDefault();
 
 		if (manager.isAccelerometerSupported()) {
 			accelerometer = manager.getAccelerometer();
 			accelerometer.setListener(this);
 		}
 	}
 
 	public void onSensorChanged(Sensor sensor, SensorData data) {
 
 		final AccelerometerData accData = (AccelerometerData) data;
 
 		// allows to update the UI
 		controller.runOnUiThread(new Runnable() {
 			public void run() {
 				// DO SOME STUFF...
 			}
 		});
 	}
 }
 

Summary

Public Methods
AccelerometerSensor getAccelerometer()
Gets an accelerometer sensor.
static SensorManager getDefault()
Returns the default SensorManager.
GyroscopeSensor getGyroscope()
Gets a gyroscope sensor.
MagnetometerSensor getMagnetometer()
Gets a magnetometer sensor.
boolean isAccelerometerSupported()
Indicates whether an accelerometer is available on the device.
boolean isGyroscopeSupported()
Indicates whether a gyroscope is available on the device.
boolean isMagnetometerSupported()
Indicates whether a magnetometer is available on the device.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public AccelerometerSensor getAccelerometer ()

Gets an accelerometer sensor.

You can get only one accelerometer per SensorManager instance. If the accelerometer is not supported then it returns a null object.

public static SensorManager getDefault ()

Returns the default SensorManager.

Returns
  • the SensorManager instance

public GyroscopeSensor getGyroscope ()

Gets a gyroscope sensor.

You can get only one gyroscope per SensorManager instance. If the gyroscope is not supported then it returns a null object.

Returns

public MagnetometerSensor getMagnetometer ()

Gets a magnetometer sensor.

You can get only one magnetometer per SensorManager instance. If the magnetometer is not supported then it returns a null object.

public boolean isAccelerometerSupported ()

Indicates whether an accelerometer is available on the device.

Returns
  • return true if the accelerometer is supported by the device.

public boolean isGyroscopeSupported ()

Indicates whether a gyroscope is available on the device.

Returns
  • true if the gyroscope is supported by the device.

public boolean isMagnetometerSupported ()

Indicates whether a magnetometer is available on the device.

Returns
  • true if the magnetometer is supported by the device.