Stay organized with collections
Save and categorize content based on your preferences.
SensorListener
public
interface
SensorListener
android.hardware.SensorListener
|
Known indirect subclasses
|
This interface was deprecated
in API level 3.
Use
SensorEventListener
instead.
Used for receiving notifications from the SensorManager when
sensor values have changed.
Summary
Public methods |
abstract
void
|
onAccuracyChanged(int sensor, int accuracy)
Called when the accuracy of a sensor has changed.
|
abstract
void
|
onSensorChanged(int sensor, float[] values)
Called when sensor values have changed.
|
Public methods
onAccuracyChanged
public abstract void onAccuracyChanged (int sensor,
int accuracy)
Called when the accuracy of a sensor has changed.
See SensorManager
for details.
Parameters |
sensor |
int : The ID of the sensor being monitored |
accuracy |
int : The new accuracy of this sensor. |
onSensorChanged
public abstract void onSensorChanged (int sensor,
float[] values)
Called when sensor values have changed.
The length and contents of the values array vary
depending on which sensor is being monitored.
See SensorManager
for details on possible sensor types.
Definition of the coordinate system used below.
The X axis refers to the screen's horizontal axis
(the small edge in portrait mode, the long edge in landscape mode) and
points to the right.
The Y axis refers to the screen's vertical axis and points towards
the top of the screen (the origin is in the lower-left corner).
The Z axis points toward the sky when the device is lying on its back
on a table.
IMPORTANT NOTE: The axis are swapped when the
device's screen orientation changes. To access the unswapped values,
use indices 3, 4 and 5 in values[].
SENSOR_ORIENTATION
,
SENSOR_ORIENTATION_RAW
:
All values are angles in degrees.
values[0]: Azimuth, rotation around the Z axis (0<=azimuth<360).
0 = North, 90 = East, 180 = South, 270 = West
values[1]: Pitch, rotation around X axis (-180<=pitch<=180), with positive
values when the z-axis moves toward the y-axis.
values[2]: Roll, rotation around Y axis (-90<=roll<=90), with positive values
when the z-axis moves toward the x-axis.
Note that this definition of yaw, pitch and roll is different from the
traditional definition used in aviation where the X axis is along the long
side of the plane (tail to nose).
SENSOR_ACCELEROMETER
:
All values are in SI units (m/s^2) and measure contact forces.
values[0]: force applied by the device on the x-axis
values[1]: force applied by the device on the y-axis
values[2]: force applied by the device on the z-axis
Examples:
When the device is pushed on its left side toward the right, the
x acceleration value is negative (the device applies a reaction force
to the push toward the left)
When the device lies flat on a table, the acceleration value is
-STANDARD_GRAVITY
,
which correspond to the force the device applies on the table in reaction
to gravity.
SENSOR_MAGNETIC_FIELD
:
All values are in micro-Tesla (uT) and measure the ambient magnetic
field in the X, Y and -Z axis.
Note: the magnetic field's Z axis is inverted.
Parameters |
sensor |
int : The ID of the sensor being monitored |
values |
float : The new values for the sensor. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# SensorListener\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nDeprecated in [API level\n3](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Methods](#pubmethods) \n\nSensorListener\n==============\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/hardware/SensorListener \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\n\ninterface\nSensorListener\n`\n\n\n`\n\n\n`\n\n|---------------------------------|\n| android.hardware.SensorListener |\n\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Known indirect subclasses [OrientationListener](/reference/android/view/OrientationListener) |--------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [OrientationListener](/reference/android/view/OrientationListener) | *This class was deprecated in API level 3. use [OrientationEventListener](/reference/android/view/OrientationEventListener) instead. This class internally uses the OrientationEventListener.* | |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\n\n**This interface was deprecated\nin API level 3.** \n\nUse\n[SensorEventListener](/reference/android/hardware/SensorEventListener) instead.\n\nUsed for receiving notifications from the SensorManager when\nsensor values have changed.\n\nSummary\n-------\n\n| ### Public methods ||\n|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract void` | ` `[onAccuracyChanged](/reference/android/hardware/SensorListener#onAccuracyChanged(int,%20int))`(int sensor, int accuracy) ` Called when the accuracy of a sensor has changed. |\n| ` abstract void` | ` `[onSensorChanged](/reference/android/hardware/SensorListener#onSensorChanged(int,%20float[]))`(int sensor, float[] values) ` \u003cbr /\u003e Called when sensor values have changed. |\n\nPublic methods\n--------------\n\n### onAccuracyChanged\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void onAccuracyChanged (int sensor, \n int accuracy)\n```\n\nCalled when the accuracy of a sensor has changed.\nSee [SensorManager](/reference/android/hardware/SensorManager)\nfor details.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------------|----------------------------------------------------|\n| `sensor` | `int`: The ID of the sensor being monitored \u003cbr /\u003e |\n| `accuracy` | `int`: The new accuracy of this sensor. \u003cbr /\u003e |\n\n### onSensorChanged\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void onSensorChanged (int sensor, \n float[] values)\n```\n\n\u003cbr /\u003e\n\nCalled when sensor values have changed.\nThe length and contents of the values array vary\ndepending on which sensor is being monitored.\nSee [SensorManager](/reference/android/hardware/SensorManager)\nfor details on possible sensor types.\n\nDefinition of the coordinate system used below.\n\n\nThe X axis refers to the screen's horizontal axis\n(the small edge in portrait mode, the long edge in landscape mode) and\npoints to the right.\n\nThe Y axis refers to the screen's vertical axis and points towards\nthe top of the screen (the origin is in the lower-left corner).\n\nThe Z axis points toward the sky when the device is lying on its back\non a table.\n\n**IMPORTANT NOTE:** The axis **are swapped** when the\ndevice's screen orientation changes. To access the unswapped values,\nuse indices 3, 4 and 5 in values\\[\\].\n\n[SENSOR_ORIENTATION](/reference/android/hardware/SensorManager#SENSOR_ORIENTATION),\n[SENSOR_ORIENTATION_RAW](/reference/android/hardware/SensorManager#SENSOR_ORIENTATION_RAW):\n\n\nAll values are angles in degrees.\n\nvalues\\[0\\]: Azimuth, rotation around the Z axis (0\\\u003c=azimuth\\\u003c360).\n0 = North, 90 = East, 180 = South, 270 = West\n\nvalues\\[1\\]: Pitch, rotation around X axis (-180\\\u003c=pitch\\\u003c=180), with positive\nvalues when the z-axis moves toward the y-axis.\n\nvalues\\[2\\]: Roll, rotation around Y axis (-90\\\u003c=roll\\\u003c=90), with positive values\nwhen the z-axis moves toward the x-axis.\n\nNote that this definition of yaw, pitch and roll is different from the\ntraditional definition used in aviation where the X axis is along the long\nside of the plane (tail to nose).\n\n[SENSOR_ACCELEROMETER](/reference/android/hardware/SensorManager#SENSOR_ACCELEROMETER):\n\n\nAll values are in SI units (m/s\\^2) and measure contact forces.\n\nvalues\\[0\\]: force applied by the device on the x-axis\n\nvalues\\[1\\]: force applied by the device on the y-axis\n\nvalues\\[2\\]: force applied by the device on the z-axis\n\nExamples:\n- When the device is pushed on its left side toward the right, the x acceleration value is negative (the device applies a reaction force to the push toward the left)\n- When the device lies flat on a table, the acceleration value is [-STANDARD_GRAVITY](/reference/android/hardware/SensorManager#STANDARD_GRAVITY), which correspond to the force the device applies on the table in reaction to gravity.\n- [SENSOR_MAGNETIC_FIELD](/reference/android/hardware/SensorManager#SENSOR_MAGNETIC_FIELD):\n- All values are in micro-Tesla (uT) and measure the ambient magnetic field in the X, Y and -Z axis.\n- **Note:** the magnetic field's Z axis is inverted.\n\n| Parameters ||\n|----------|----------------------------------------------------|\n| `sensor` | `int`: The ID of the sensor being monitored \u003cbr /\u003e |\n| `values` | `float`: The new values for the sensor. \u003cbr /\u003e |"]]