Android Guides | Samples

Android.Hardware.SensorManager Class

java Example

 public class SensorActivity extends Activity, implements SensorEventListener {
     private final SensorManager mSensorManager;
     private final Sensor mAccelerometer;

     public SensorActivity() {
         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     }

     protected void onResume() {
         super.onResume();
         mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
     }

     protected void onPause() {
         super.onPause();
         mSensorManager.unregisterListener(this);
     }

     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }

     public void onSensorChanged(SensorEvent event) {
     }
 }

See Also: SensorManager

Syntax

[Android.Runtime.Register("android/hardware/SensorManager", DoNotGenerateAcw=true)]
public abstract class SensorManager : Object

Remarks

java Example

 public class SensorActivity extends Activity, implements SensorEventListener {
     private final SensorManager mSensorManager;
     private final Sensor mAccelerometer;

     public SensorActivity() {
         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     }

     protected void onResume() {
         super.onResume();
         mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
     }

     protected void onPause() {
         super.onPause();
         mSensorManager.unregisterListener(this);
     }

     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }

     public void onSensorChanged(SensorEvent event) {
     }
 }

SensorManager lets you access the device's Sensor. Get an instance of this class by calling Context.GetSystemService(String) with the argument Context.SensorService.

Always make sure to disable sensors you don't need, especially when your activity 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.

Note: Don't use this mechanism with a Trigger Sensor, have a look at TriggerEventListener. SensorType.SignificantMotion is an example of a trigger sensor.

See Also

[Android Documentation]

Requirements

Namespace: Android.Hardware
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Android.Hardware.SensorManager are listed below.

See Also: Object

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Fields

const
GravityDeathStarISingle (0). Gravity (estimate) on the first Death Star in Empire units (m/s^2)
const
GravityEarthSingle (9.80665). Earth's gravity in SI units (m/s^2)
const
GravityJupiterSingle (23.12). Jupiter's gravity in SI units (m/s^2)
const
GravityMarsSingle (3.71). Mars' gravity in SI units (m/s^2)
const
GravityMercurySingle (3.7). Mercury's gravity in SI units (m/s^2)
const
GravityMoonSingle (1.6). The Moon's gravity in SI units (m/s^2)
const
GravityNeptuneSingle (11). Neptune's gravity in SI units (m/s^2)
const
GravityPlutoSingle (0.6). Pluto's gravity in SI units (m/s^2)
const
GravitySaturnSingle (8.96). Saturn's gravity in SI units (m/s^2)
const
GravitySunSingle (275). Sun's gravity in SI units (m/s^2)
const
GravityTheIslandSingle (4.815162). Gravity on the island
const
GravityUranusSingle (8.69). Uranus' gravity in SI units (m/s^2)
const
GravityVenusSingle (8.87). Venus' gravity in SI units (m/s^2)
const
LightCloudySingle (100). luminance under a cloudy sky in lux
const
LightFullmoonSingle (0.25). luminance at night with full moon in lux
const
LightNoMoonSingle (0.001). luminance at night with no moon in lux
const
LightOvercastSingle (10000). luminance under an overcast sky in lux
const
LightShadeSingle (20000). luminance in shade in lux
const
LightSunlightSingle (110000). luminance of sunlight in lux
const
LightSunlightMaxSingle (120000). Maximum luminance of sunlight in lux
const
LightSunriseSingle (400). luminance at sunrise in lux
const
MagneticFieldEarthMaxSingle (60). Maximum magnetic field on Earth's surface
const
MagneticFieldEarthMinSingle (30). Minimum magnetic field on Earth's surface
const
PressureStandardAtmosphereSingle (1013.25). Standard atmosphere, or average sea-level pressure in hPa (millibar)
const
StandardGravitySingle (9.80665). Standard gravity (g) on Earth.

Public Properties

[read-only]
SensorsInt32.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

CancelTriggerSensor(TriggerEventListener, Sensor) : Boolean
Cancels receiving trigger events for a trigger sensor.
Flush(ISensorEventListener) : Boolean
Flushes the FIFO of all the sensors registered for this listener.
static
FromContext(Context) : SensorManager
static
GetAltitude(Single, Single) : Single
Computes the Altitude in meters from the atmospheric pressure and the pressure at sea level.
static
GetAngleChange(Single[], Single[], Single[])
Helper function to compute the angle change between two rotation matrices.
GetDefaultSensor(SensorType) : Sensor
Use this method to get the default sensor for a given type.
GetDefaultSensor(SensorType, Boolean) : Sensor
Return a Sensor with the given type and wakeUp properties.
static
GetInclination(Single[]) : Single
Computes the geomagnetic inclination angle in radians from the inclination matrix I returned by SensorManager.GetRotationMatrix(Single[],Single[],Single[],Single[]).
static
GetOrientation(Single[], Single[]) : Single[]
Computes the device's orientation based on the rotation matrix.
static
GetQuaternionFromVector(Single[], Single[])
Helper function to convert a rotation vector to a normalized quaternion.
static
GetRotationMatrix(Single[], Single[], Single[], Single[]) : Boolean
static
GetRotationMatrixFromVector(Single[], Single[])
Helper function to convert a rotation vector to a rotation matrix.
GetSensorList(SensorType) : IList<Sensor>
Use this method to get the list of available sensors of a certain type.
RegisterListener(ISensorListener, Int32) : Boolean
Registers a listener for given sensors.
RegisterListener(ISensorEventListener, Sensor, SensorDelay) : Boolean
Registers a ISensorEventListener for the given sensor at the given sampling frequency.
RegisterListener(ISensorListener, Int32, SensorDelay) : Boolean
Registers a SensorListener for given sensors.
RegisterListener(ISensorEventListener, Sensor, SensorDelay, Handler) : Boolean
Registers a ISensorEventListener for the given sensor.
RegisterListener(ISensorEventListener, Sensor, SensorDelay, Int32) : Boolean
Registers a ISensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.
RegisterListener(ISensorEventListener, Sensor, Int32, Int32) : Boolean
Registers a ISensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.
RegisterListener(ISensorEventListener, Sensor, SensorDelay, Int32, Handler) : Boolean
Registers a ISensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.
RegisterListener(ISensorEventListener, Sensor, Int32, Int32, Handler) : Boolean
Registers a ISensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.
static
RemapCoordinateSystem(Single[], Axis, Axis, Single[]) : Boolean
RequestTriggerSensor(TriggerEventListener, Sensor) : Boolean
Requests receiving trigger events for a trigger sensor.
UnregisterListener(ISensorEventListener)
Unregisters a listener for all sensors.
UnregisterListener(ISensorListener)
Unregisters a listener for all sensors.
UnregisterListener(ISensorEventListener, Sensor)
Unregisters a listener for the sensors with which it is registered.
UnregisterListener(ISensorListener, Int32)
Unregisters a listener for the sensors with which it is registered.