Most visited

Recently visited

Added in API level 1

SensorManager

public abstract class SensorManager
extends Object

java.lang.Object
   ↳ android.hardware.SensorManager


SensorManager允许您访问设备的sensors 通过调用参数SENSOR_SERVICE Context.getSystemService()来获得SENSOR_SERVICE

务必确保禁用不需要的传感器,尤其是当您的活动暂停时。 如果不这样做,可能会在几个小时内耗尽电池。 请注意,当屏幕关闭时,系统不会自动禁用传感器。

注意:请勿将此机制与触发传感器一起使用,请查看TriggerEventListener TYPE_SIGNIFICANT_MOTION是触发传感器的一个例子。

 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) {
     }
 }
 

也可以看看:

Summary

Nested classes

class SensorManager.DynamicSensorCallback

用于在动态传感器连接或断开连接时从SensorManager接收通知。

Constants

int AXIS_MINUS_X

remapCoordinateSystem(float[], int, int, float[])

int AXIS_MINUS_Y

remapCoordinateSystem(float[], int, int, float[])

int AXIS_MINUS_Z

remapCoordinateSystem(float[], int, int, float[])

int AXIS_X

remapCoordinateSystem(float[], int, int, float[])

int AXIS_Y

remapCoordinateSystem(float[], int, int, float[])

int AXIS_Z

remapCoordinateSystem(float[], int, int, float[])

int DATA_X

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int DATA_Y

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int DATA_Z

此常数在API级别3中已弃用。 Sensor改为使用Sensor

float GRAVITY_DEATH_STAR_I

帝国单位第一颗死星的重力(估算值)(m / s ^ 2)

float GRAVITY_EARTH

地球重力SI单位(m / s ^ 2)

float GRAVITY_JUPITER

木星以SI单位表示的重力(m / s ^ 2)

float GRAVITY_MARS

火星的SI单位引力(m / s ^ 2)

float GRAVITY_MERCURY

水星的SI重力(m / s ^ 2)

float GRAVITY_MOON

月球的SI单位重力(m / s ^ 2)

float GRAVITY_NEPTUNE

海王星的国际单位制重力(m / s ^ 2)

float GRAVITY_PLUTO

冥王星以SI单位的重力(m / s ^ 2)

float GRAVITY_SATURN

土星的国际单位重力(m / s ^ 2)

float GRAVITY_SUN

太阳的SI单位的重力(m / s ^ 2)

float GRAVITY_THE_ISLAND

重力在岛上

float GRAVITY_URANUS

天王星以SI单位的重力(m / s ^ 2)

float GRAVITY_VENUS

维纳斯的SI单位引力(m / s ^ 2)

float LIGHT_CLOUDY

亮度在勒克斯的多云天空下

float LIGHT_FULLMOON

夜间亮度与满月在勒克斯

float LIGHT_NO_MOON

晚上的亮度和勒克斯没有月亮

float LIGHT_OVERCAST

亮度在勒克斯的阴天下

float LIGHT_SHADE

亮度在勒克斯的阴影中

float LIGHT_SUNLIGHT

勒克斯的阳光照度

float LIGHT_SUNLIGHT_MAX

以勒克斯为单位的最大阳光照度

float LIGHT_SUNRISE

在日出时的亮度在勒克斯

float MAGNETIC_FIELD_EARTH_MAX

地球表面的最大磁场

float MAGNETIC_FIELD_EARTH_MIN

地球表面的最小磁场

float PRESSURE_STANDARD_ATMOSPHERE

标准大气压,或平均海平面气压(hPa)(毫巴)

int RAW_DATA_INDEX

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int RAW_DATA_X

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int RAW_DATA_Y

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int RAW_DATA_Z

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_ACCELEROMETER

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_ALL

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_DELAY_FASTEST

尽可能快地获取传感器数据

int SENSOR_DELAY_GAME

适合游戏的价格

int SENSOR_DELAY_NORMAL

率(默认)适合屏幕方向更改

int SENSOR_DELAY_UI

适合用户界面的速率

int SENSOR_LIGHT

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_MAGNETIC_FIELD

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_MAX

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_MIN

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_ORIENTATION

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_ORIENTATION_RAW

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_PROXIMITY

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_STATUS_ACCURACY_HIGH

该传感器以最高精度报告数据

int SENSOR_STATUS_ACCURACY_LOW

该传感器报告的数据精度较低,需要与环境进行校准

int SENSOR_STATUS_ACCURACY_MEDIUM

该传感器报告的数据具有平均的准确度,与环境校准可能会提高读数

int SENSOR_STATUS_NO_CONTACT

这个传感器返回的值是不可信的,因为传感器没有接触到它正在测量的内容(例如,心率监测器没有与用户接触)。

int SENSOR_STATUS_UNRELIABLE

该传感器返回的值不可信,需要校准或环境不允许读数

int SENSOR_TEMPERATURE

此常数在API级别3中已弃用。 Sensor改为使用Sensor

int SENSOR_TRICORDER

此常数在API级别3中已弃用。 Sensor改为使用Sensor

float STANDARD_GRAVITY

标准重力(g)在地球上。

Public methods

boolean cancelTriggerSensor(TriggerEventListener listener, Sensor sensor)

取消接收触发传感器的触发事件。

boolean flush(SensorEventListener listener)

为所有为此侦听器注册的传感器刷新FIFO。

static float getAltitude(float p0, float p)

计算大气压和海平面压力的米数。

static void getAngleChange(float[] angleChange, float[] R, float[] prevR)

Helper函数计算两个旋转矩阵之间的角度变化。

Sensor getDefaultSensor(int type)

使用此方法获取给定类型的默认传感器。

Sensor getDefaultSensor(int type, boolean wakeUp)

使用给定的类型和唤醒属性返回传感器。

List<Sensor> getDynamicSensorList(int type)

使用此方法可获取某种类型的可用动态传感器的列表。

static float getInclination(float[] I)

计算由 getRotationMatrix(float[], float[], float[], float[])返回的倾斜矩阵 I的弧度的地磁倾角。

static float[] getOrientation(float[] R, float[] values)

根据旋转矩阵计算设备的方向。

static void getQuaternionFromVector(float[] Q, float[] rv)

Helper函数将旋转矢量转换为归一化四元数。

static boolean getRotationMatrix(float[] R, float[] I, float[] gravity, float[] geomagnetic)

计算倾斜矩阵 I以及旋转矩阵 R,将矢量从设备坐标系转换为定义为直接标准正交基的世界坐标系,其中:

  • X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).

static void getRotationMatrixFromVector(float[] R, float[] rotationVector)

Helper函数将旋转向量转换为旋转矩阵。

List<Sensor> getSensorList(int type)

使用此方法获取特定类型的可用传感器列表。

int getSensors()

此方法在API级别3中已弃用。此方法已弃用, getSensorList(int)改为使用getSensorList(int)

boolean isDynamicSensorDiscoverySupported()

告诉系统是否支持动态传感器发现功能。

void registerDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)

添加 DynamicSensorCallback以接收动态传感器连接回调。

void registerDynamicSensorCallback(SensorManager.DynamicSensorCallback callback, Handler handler)

添加一个 DynamicSensorCallback以接收动态传感器连接回调。

boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs)

以给定的采样频率为给定的传感器注册 SensorEventListener

boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxReportLatencyUs)

以给定的采样频率和给定的最大报告延迟为给定传感器注册 SensorEventListener

boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, Handler handler)

为给定的传感器注册一个 SensorEventListener

boolean registerListener(SensorListener listener, int sensors)

此方法在API级别3中已弃用。此方法已弃用, registerListener(SensorEventListener, Sensor, int)改为使用registerListener(SensorEventListener, Sensor, int)

boolean registerListener(SensorListener listener, int sensors, int rate)

此方法在API级别3中已弃用。此方法已弃用, registerListener(SensorEventListener, Sensor, int)改为使用registerListener(SensorEventListener, Sensor, int)

boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxReportLatencyUs, Handler handler)

以给定的采样频率和给定的最大报告延迟为给定传感器注册 SensorEventListener

static boolean remapCoordinateSystem(float[] inR, int X, int Y, float[] outR)

旋转提供的旋转矩阵,以便在不同的坐标系中表示。

boolean requestTriggerSensor(TriggerEventListener listener, Sensor sensor)

请求接收触发传感器的触发事件。

void unregisterDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)

删除 DynamicSensorCallback以停止向该回调发送动态传感器连接事件。

void unregisterListener(SensorEventListener listener)

取消注册所有传感器的监听器。

void unregisterListener(SensorEventListener listener, Sensor sensor)

取消注册与其注册的传感器的监听器。

void unregisterListener(SensorListener listener)

此方法在API级别3中已弃用。此方法已弃用, unregisterListener(SensorEventListener)改为使用unregisterListener(SensorEventListener)

void unregisterListener(SensorListener listener, int sensors)

此方法在API级别3中已弃用。此方法已弃用, unregisterListener(SensorEventListener, Sensor)改为使用unregisterListener(SensorEventListener, Sensor)

Inherited methods

From class java.lang.Object

Constants

AXIS_MINUS_X

Added in API level 3
int AXIS_MINUS_X

remapCoordinateSystem(float[], int, int, float[])

常量值:129(0x00000081)

AXIS_MINUS_Y

Added in API level 3
int AXIS_MINUS_Y

remapCoordinateSystem(float[], int, int, float[])

常量值:130(0x00000082)

AXIS_MINUS_Z

Added in API level 3
int AXIS_MINUS_Z

remapCoordinateSystem(float[], int, int, float[])

常量值:131(0x00000083)

AXIS_X

Added in API level 3
int AXIS_X

remapCoordinateSystem(float[], int, int, float[])

常数值:1(0x00000001)

AXIS_Y

Added in API level 3
int AXIS_Y

remapCoordinateSystem(float[], int, int, float[])

常量值:2(0x00000002)

AXIS_Z

Added in API level 3
int AXIS_Z

remapCoordinateSystem(float[], int, int, float[])

常量值:3(0x00000003)

DATA_X

Added in API level 1
int DATA_X

此常数在API级别3中已弃用。
改为使用Sensor

数组中X值的索引由 onSensorChanged(int, float[])返回

常量值:0(0x00000000)

DATA_Y

Added in API level 1
int DATA_Y

此常数在API级别3中已弃用。
改为使用Sensor

数组中Y值的索引由 onSensorChanged(int, float[])返回

常数值:1(0x00000001)

DATA_Z

Added in API level 1
int DATA_Z

此常数在API级别3中已弃用。
改为使用Sensor

数组中Z值的索引由 onSensorChanged(int, float[])返回

常量值:2(0x00000002)

GRAVITY_DEATH_STAR_I

Added in API level 1
float GRAVITY_DEATH_STAR_I

帝国单位第一颗死星的重力(估算值)(m / s ^ 2)

常数值:3.5303614E-7

GRAVITY_EARTH

Added in API level 1
float GRAVITY_EARTH

地球重力SI单位(m / s ^ 2)

常数值:9.80665

GRAVITY_JUPITER

Added in API level 1
float GRAVITY_JUPITER

木星以SI单位表示的重力(m / s ^ 2)

常数值:23.12

GRAVITY_MARS

Added in API level 1
float GRAVITY_MARS

火星的SI单位引力(m / s ^ 2)

常数值:3.71

GRAVITY_MERCURY

Added in API level 1
float GRAVITY_MERCURY

水星的SI重力(m / s ^ 2)

常数值:3.7

GRAVITY_MOON

Added in API level 1
float GRAVITY_MOON

月球的SI单位重力(m / s ^ 2)

常数值:1.6

GRAVITY_NEPTUNE

Added in API level 1
float GRAVITY_NEPTUNE

海王星的国际单位制重力(m / s ^ 2)

常数值:11.0

GRAVITY_PLUTO

Added in API level 1
float GRAVITY_PLUTO

冥王星以SI单位的重力(m / s ^ 2)

常数值:0.6

GRAVITY_SATURN

Added in API level 1
float GRAVITY_SATURN

土星的国际单位重力(m / s ^ 2)

常数值:8.96

GRAVITY_SUN

Added in API level 1
float GRAVITY_SUN

太阳的SI单位的重力(m / s ^ 2)

常数值:275.0

GRAVITY_THE_ISLAND

Added in API level 1
float GRAVITY_THE_ISLAND

重力在岛上

常数值:4.815162

GRAVITY_URANUS

Added in API level 1
float GRAVITY_URANUS

天王星以SI单位的重力(m / s ^ 2)

常数值:8.69

GRAVITY_VENUS

Added in API level 1
float GRAVITY_VENUS

维纳斯的SI单位引力(m / s ^ 2)

常数值:8.87

LIGHT_CLOUDY

Added in API level 1
float LIGHT_CLOUDY

亮度在勒克斯的多云天空下

常数值:100.0

LIGHT_FULLMOON

Added in API level 1
float LIGHT_FULLMOON

夜间亮度与满月在勒克斯

常数值:0.25

LIGHT_NO_MOON

Added in API level 1
float LIGHT_NO_MOON

晚上的亮度和勒克斯没有月亮

常数值:0.001

LIGHT_OVERCAST

Added in API level 1
float LIGHT_OVERCAST

亮度在勒克斯的阴天下

常数值:10000.0

LIGHT_SHADE

Added in API level 1
float LIGHT_SHADE

亮度在勒克斯的阴影中

常量值:20000.0

LIGHT_SUNLIGHT

Added in API level 1
float LIGHT_SUNLIGHT

勒克斯的阳光照度

常数值:110000.0

LIGHT_SUNLIGHT_MAX

Added in API level 1
float LIGHT_SUNLIGHT_MAX

以勒克斯为单位的最大阳光照度

常数值:120000.0

LIGHT_SUNRISE

Added in API level 1
float LIGHT_SUNRISE

在日出时的亮度在勒克斯

常数值:400.0

MAGNETIC_FIELD_EARTH_MAX

Added in API level 1
float MAGNETIC_FIELD_EARTH_MAX

地球表面的最大磁场

常数值:60.0

MAGNETIC_FIELD_EARTH_MIN

Added in API level 1
float MAGNETIC_FIELD_EARTH_MIN

地球表面的最小磁场

常数值:30.0

PRESSURE_STANDARD_ATMOSPHERE

Added in API level 9
float PRESSURE_STANDARD_ATMOSPHERE

标准大气压,或平均海平面气压(hPa)(毫巴)

常数值:1013.25

RAW_DATA_INDEX

Added in API level 1
int RAW_DATA_INDEX

此常数在API级别3中已弃用。
改为使用Sensor

偏移到由 onSensorChanged(int, float[])返回的数组中未转换的值

常量值:3(0x00000003)

RAW_DATA_X

Added in API level 1
int RAW_DATA_X

此常数在API级别3中已弃用。
改为使用Sensor

数组中未转换的X值的索引由 onSensorChanged(int, float[])返回

常量值:3(0x00000003)

RAW_DATA_Y

Added in API level 1
int RAW_DATA_Y

此常数在API级别3中已弃用。
改为使用Sensor

数组中未转换的Y值的索引由 onSensorChanged(int, float[])返回

常量值:4(0x00000004)

RAW_DATA_Z

Added in API level 1
int RAW_DATA_Z

此常数在API级别3中已弃用。
改为使用Sensor

数组中未转换的Z值的索引由 onSensorChanged(int, float[])返回

常量值:5(0x00000005)

SENSOR_ACCELEROMETER

Added in API level 1
int SENSOR_ACCELEROMETER

此常数在API级别3中已弃用。
改为使用Sensor

描述加速度计的常量。 有关更多详细信息,请参阅SensorListener

常量值:2(0x00000002)

SENSOR_ALL

Added in API level 1
int SENSOR_ALL

此常数在API级别3中已弃用。
改为使用Sensor

包含所有传感器的常量

常量值:127(0x0000007f)

SENSOR_DELAY_FASTEST

Added in API level 1
int SENSOR_DELAY_FASTEST

尽可能快地获取传感器数据

常量值:0(0x00000000)

SENSOR_DELAY_GAME

Added in API level 1
int SENSOR_DELAY_GAME

适合游戏的价格

常数值:1(0x00000001)

SENSOR_DELAY_NORMAL

Added in API level 1
int SENSOR_DELAY_NORMAL

率(默认)适合屏幕方向更改

常量值:3(0x00000003)

SENSOR_DELAY_UI

Added in API level 1
int SENSOR_DELAY_UI

适合用户界面的速率

常量值:2(0x00000002)

SENSOR_LIGHT

Added in API level 1
int SENSOR_LIGHT

此常数在API级别3中已弃用。
改为使用Sensor

描述环境光线传感器的常量请参阅 SensorListener了解更多详情。

常量值:16(0x00000010)

SENSOR_MAGNETIC_FIELD

Added in API level 1
int SENSOR_MAGNETIC_FIELD

此常数在API级别3中已弃用。
改为使用Sensor

描述磁性传感器的常量请参阅 SensorListener了解更多详情。

常量值:8(0x00000008)

SENSOR_MAX

Added in API level 1
int SENSOR_MAX

此常数在API级别3中已弃用。
改为使用Sensor

最大的传感器ID

常量值:64(0x00000040)

SENSOR_MIN

Added in API level 1
int SENSOR_MIN

此常数在API级别3中已弃用。
改为使用Sensor

最小的传感器ID

常数值:1(0x00000001)

SENSOR_ORIENTATION

Added in API level 1
int SENSOR_ORIENTATION

此常数在API级别3中已弃用。
改为使用Sensor

描述方位传感器的常量。 有关更多详细信息,请参阅SensorListener

常数值:1(0x00000001)

SENSOR_ORIENTATION_RAW

Added in API level 1
int SENSOR_ORIENTATION_RAW

此常数在API级别3中已弃用。
改为使用Sensor

描述方位传感器的常量。 有关更多详细信息,请参阅SensorListener

常量值:128(0x00000080)

SENSOR_PROXIMITY

Added in API level 1
int SENSOR_PROXIMITY

此常数在API级别3中已弃用。
改为使用Sensor

描述接近传感器的 SensorListener有关更多详细信息,请参阅 SensorListener

常量值:32(0x00000020)

SENSOR_STATUS_ACCURACY_HIGH

Added in API level 1
int SENSOR_STATUS_ACCURACY_HIGH

该传感器以最高精度报告数据

常量值:3(0x00000003)

SENSOR_STATUS_ACCURACY_LOW

Added in API level 1
int SENSOR_STATUS_ACCURACY_LOW

该传感器报告的数据精度较低,需要与环境进行校准

常数值:1(0x00000001)

SENSOR_STATUS_ACCURACY_MEDIUM

Added in API level 1
int SENSOR_STATUS_ACCURACY_MEDIUM

该传感器报告的数据具有平均的准确度,与环境校准可能会提高读数

常量值:2(0x00000002)

SENSOR_STATUS_NO_CONTACT

Added in API level 20
int SENSOR_STATUS_NO_CONTACT

这个传感器返回的值是不可信的,因为传感器没有接触到它正在测量的内容(例如,心率监测器没有与用户接触)。

常量值:-1(0xffffffff)

SENSOR_STATUS_UNRELIABLE

Added in API level 1
int SENSOR_STATUS_UNRELIABLE

该传感器返回的值不可信,需要校准或环境不允许读数

常量值:0(0x00000000)

SENSOR_TEMPERATURE

Added in API level 1
int SENSOR_TEMPERATURE

此常数在API级别3中已弃用。
改为使用Sensor

描述温度传感器的常量请参阅 SensorListener了解更多详情。

常量值:4(0x00000004)

SENSOR_TRICORDER

Added in API level 1
int SENSOR_TRICORDER

此常数在API级别3中已弃用。
改为使用Sensor

描述Tricorder的常量请参阅 SensorListener了解更多详情。

常量值:64(0x00000040)

STANDARD_GRAVITY

Added in API level 1
float STANDARD_GRAVITY

标准重力(g)在地球上。 这个值相当于1G

常数值:9.80665

Public methods

cancelTriggerSensor

Added in API level 18
boolean cancelTriggerSensor (TriggerEventListener listener, 
                Sensor sensor)

取消接收触发传感器的触发事件。

请注意,如果onTrigger(TriggerEvent)已被触发,触发传感器将被自动禁用。 如果用户想明确取消接收触发事件的请求,则提供此方法。

Parameters
listener TriggerEventListener: The listener on which the onTrigger(TriggerEvent) is delivered.It should be the same as the one used in requestTriggerSensor(TriggerEventListener, Sensor)
sensor Sensor: The sensor for which the trigger request should be canceled. If null, it cancels receiving trigger for all sensors associated with the listener.
Returns
boolean true if successfully canceled.
Throws
IllegalArgumentException when sensor is a trigger sensor.

flush

Added in API level 19
boolean flush (SensorEventListener listener)

为所有为此侦听器注册的传感器刷新FIFO。 如果传感器的FIFO中有事件,则返回它们,就好像FIFO的maxReportLantecy已经过期一样。 事件以通常的方式通过SensorEventListener返回。 此调用不会影响此传感器的maxReportLantecy。 这个调用是异步的,并立即返回。 在调用此方法时批处理中的所有事件已成功传送后调用onFlushCompleted 如果硬件不支持刷新,它仍然返回true,并且在注册此传感器的所有客户端的当前事件之后发送平凡刷新完成事件。

Parameters
listener SensorEventListener: A SensorEventListener object which was previously used in a registerListener call.
Returns
boolean true if the flush is initiated successfully on all the sensors registered for this listener, false if no sensor is previously registered for this listener or flush on one of the sensors fails.
Throws
IllegalArgumentException when listener is null.

也可以看看:

getAltitude

Added in API level 9
float getAltitude (float p0, 
                float p)

计算大气压和海平面压力的米数。

通常从TYPE_PRESSURE传感器读取大气压力。 海平面的压力必须是已知的,通常可以从附近的机场数据库中检索。 如果不知道,可以使用PRESSURE_STANDARD_ATMOSPHERE作为近似值,但绝对高度不准确。

要计算高度差异,您必须计算两点高度之间的差异。 如果您不知道海拔高度为海平面,则可以使用PRESSURE_STANDARD_ATMOSPHERE ,考虑到通常涉及的压力范围,这将给出良好的结果。

    float altitude_difference = getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_at_point2) - getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_at_point1);

Parameters
p0 float: pressure at sea level
p float: atmospheric pressure
Returns
float Altitude in meters

getAngleChange

Added in API level 9
void getAngleChange (float[] angleChange, 
                float[] R, 
                float[] prevR)

Helper函数计算两个旋转矩阵之间的角度变化。 给定一个当前旋转矩阵(R)和一个先前旋转矩阵(prevR),计算围绕z轴,x轴和y轴的固有旋转,将prevR转换为R.输出包含z,x和y角度变化的3元素向量分别在索引0,1和2处。

每个输入矩阵可以是3x3或4x4行主矩阵,具体取决于传递数组的长度:

如果数组长度为9,则数组元素表示此矩阵

   /  R[ 0]   R[ 1]   R[ 2]   \
   |  R[ 3]   R[ 4]   R[ 5]   |
   \  R[ 6]   R[ 7]   R[ 8]   /

如果数组长度是16,那么数组元素表示这个矩阵

   /  R[ 0]   R[ 1]   R[ 2]   R[ 3]  \
   |  R[ 4]   R[ 5]   R[ 6]   R[ 7]  |
   |  R[ 8]   R[ 9]   R[10]   R[11]  |
   \  R[12]   R[13]   R[14]   R[15]  /
See getOrientation(float[], float[]) for more detailed definition of the output.

Parameters
angleChange float: an an array of floats (z, x, and y) in which the angle change (in radians) is stored
R float: current rotation matrix
prevR float: previous rotation matrix

getDefaultSensor

Added in API level 3
Sensor getDefaultSensor (int type)

使用此方法获取给定类型的默认传感器。 请注意,返回的传感器可能是一个复合传感器,其数据可以进行平均或过滤。 如果您需要访问原始传感器,请使用getSensorList

Parameters
type int: of sensors requested
Returns
Sensor the default sensor matching the requested type if one exists and the application has the necessary permissions, or null otherwise.

也可以看看:

getDefaultSensor

Added in API level 21
Sensor getDefaultSensor (int type, 
                boolean wakeUp)

使用给定的类型和唤醒属性返回传感器。 如果存在多种这种类型的传感器,则可以返回它们中的任何一个。

例如,

注意:默认情况下, TYPE_PROXIMITYTYPE_SIGNIFICANT_MOTION等传感器被声明为唤醒传感器。

Parameters
type int: type of sensor requested
wakeUp boolean: flag to indicate whether the Sensor is a wake-up or non wake-up sensor.
Returns
Sensor the default sensor matching the requested type and wakeUp properties if one exists and the application has the necessary permissions, or null otherwise.

也可以看看:

getDynamicSensorList

Added in API level 24
List<Sensor> getDynamicSensorList (int type)

使用此方法可获取某种类型的可用动态传感器的列表。 进行多次调用以获取不同类型的传感器,或使用Sensor.TYPE_ALL获取所有动态传感器。

注意:返回与给定类型匹配的唤醒和非唤醒传感器。 检查isWakeUpSensor()以了解返回的Sensor的唤醒属性。

Parameters
type int: of sensors requested
Returns
List<Sensor> a list of dynamic sensors matching the requested type.

也可以看看:

getInclination

Added in API level 3
float getInclination (float[] I)

计算由 getRotationMatrix(float[], float[], float[], float[])返回的倾斜矩阵 I的弧度的地磁倾角。

Parameters
I float: inclination matrix see getRotationMatrix(float[], float[], float[], float[]).
Returns
float The geomagnetic inclination angle in radians.

也可以看看:

getOrientation

Added in API level 3
float[] getOrientation (float[] R, 
                float[] values)

根据旋转矩阵计算设备的方向。

当它返回时,数组值如下所示:

  • values[0]: Azimuth, angle of rotation about the -z axis. This value represents the angle between the device's y axis and the magnetic north pole. When facing north, this angle is 0, when facing south, this angle is π. Likewise, when facing east, this angle is π/2, and when facing west, this angle is -π/2. The range of values is -π to π.
  • values[1]: Pitch, angle of rotation about the x axis. This value represents the angle between a plane parallel to the device's screen and a plane parallel to the ground. Assuming that the bottom edge of the device faces the user and that the screen is face-up, tilting the top edge of the device toward the ground creates a positive pitch angle. The range of values is -π to π.
  • values[2]: Roll, angle of rotation about the y axis. This value represents the angle between a plane perpendicular to the device's screen and a plane perpendicular to the ground. Assuming that the bottom edge of the device faces the user and that the screen is face-up, tilting the left edge of the device toward the ground creates a positive roll angle. The range of values is -π/2 to π/2.

在方位角,俯仰角,滚动顺序中应用这三个旋转将单位矩阵转换为传递给该方法的旋转矩阵。 另外,请注意,所有三个方位角均以弧度表示

Parameters
R float: rotation matrix see getRotationMatrix(float[], float[], float[], float[]).
values float: an array of 3 floats to hold the result.
Returns
float[] The array values passed as argument.

也可以看看:

getQuaternionFromVector

Added in API level 9
void getQuaternionFromVector (float[] Q, 
                float[] rv)

Helper函数将旋转矢量转换为归一化四元数。 给定一个旋转向量(推测来自ROTATION_VECTOR传感器),返回数组Q中的归一化四元数。四元数存储为[w,x,y,z]

Parameters
Q float: an array of floats in which to store the computed quaternion
rv float: the rotation vector to convert

getRotationMatrix

Added in API level 3
boolean getRotationMatrix (float[] R, 
                float[] I, 
                float[] gravity, 
                float[] geomagnetic)

计算倾斜矩阵 I以及旋转矩阵 R,将矢量从设备坐标系转换为定义为直接标准正交基的世界坐标系,其中:

  • X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).
  • Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
  • Z points towards the sky and is perpendicular to the ground.

World coordinate-system diagram.


根据定义:

[0 0 g] = R * 重力 (g =重力的大小)

[0 m 0] = I * R * 地磁 (m =地磁场的大小)

R是设备与世界坐标系对齐时的单位矩阵,即设备的X轴指向东方时,Y轴指向北极并且设备面向天空。

是一个旋转矩阵,将地磁矢量转换为与重力(世界坐标空间)相同的坐标空间。 是围绕X轴的简单旋转。 以弧度表示的倾斜角度可以用getInclination(float[])来计算。


每个矩阵根据传递数组的长度以3x3或4x4行主矩阵的形式返回:

如果数组长度是16:

   /  M[ 0]   M[ 1]   M[ 2]   M[ 3]  \
   |  M[ 4]   M[ 5]   M[ 6]   M[ 7]  |
   |  M[ 8]   M[ 9]   M[10]   M[11]  |
   \  M[12]   M[13]   M[14]   M[15]  /
This matrix is ready to be used by OpenGL ES's glLoadMatrixf(float[], int).

请注意,因为OpenGL矩阵是列主矩阵,所以在使用矩阵之前必须对矩阵进行转置。 然而,由于矩阵是一个旋转矩阵,它的转置也是它的逆矩阵,方便地,它通常是渲染所需的旋转的逆矩阵; 因此可以直接与OpenGL ES一起使用。

还要注意,返回的矩阵总是具有这种形式:

   /  M[ 0]   M[ 1]   M[ 2]   0  \
   |  M[ 4]   M[ 5]   M[ 6]   0  |
   |  M[ 8]   M[ 9]   M[10]   0  |
   \      0       0       0   1  /

如果数组长度为9:

   /  M[ 0]   M[ 1]   M[ 2]  \
   |  M[ 3]   M[ 4]   M[ 5]  |
   \  M[ 6]   M[ 7]   M[ 8]  /

每个矩阵的逆可以通过其转置容易地计算。

这个函数返回的矩阵只有在设备没有自由落体而且不接近磁北时才有意义。 如果设备正在加速,或置于强磁场中,则返回的矩阵可能不准确。

Parameters
R float: is an array of 9 floats holding the rotation matrix R when this function returns. R can be null.

I float: is an array of 9 floats holding the rotation matrix I when this function returns. I can be null.

gravity float: is an array of 3 floats containing the gravity vector expressed in the device's coordinate. You can simply use the values returned by a SensorEvent of a Sensor of type TYPE_ACCELEROMETER.

geomagnetic float: is an array of 3 floats containing the geomagnetic vector expressed in the device's coordinate. You can simply use the values returned by a SensorEvent of a Sensor of type TYPE_MAGNETIC_FIELD.
Returns
boolean true on success, false on failure (for instance, if the device is in free fall). Free fall is defined as condition when the magnitude of the gravity is less than 1/10 of the nominal value. On failure the output matrices are not modified.

也可以看看:

getRotationMatrixFromVector

Added in API level 9
void getRotationMatrixFromVector (float[] R, 
                float[] rotationVector)

Helper函数将旋转向量转换为旋转矩阵。 给定一个旋转向量(大概来自ROTATION_VECTOR传感器),返回数组R中的9或16个元素旋转矩阵.R必须具有长度9或16.如果R.length == 9,则返回以下矩阵:

   /  R[ 0]   R[ 1]   R[ 2]   \
   |  R[ 3]   R[ 4]   R[ 5]   |
   \  R[ 6]   R[ 7]   R[ 8]   /
If R.length == 16, the following matrix is returned:
   /  R[ 0]   R[ 1]   R[ 2]   0  \
   |  R[ 4]   R[ 5]   R[ 6]   0  |
   |  R[ 8]   R[ 9]   R[10]   0  |
   \  0       0       0       1  /

Parameters
R float: an array of floats in which to store the rotation matrix
rotationVector float: the rotation vector to convert

getSensorList

Added in API level 3
List<Sensor> getSensorList (int type)

使用此方法获取特定类型的可用传感器列表。 进行多次调用以获取不同类型的传感器,或使用Sensor.TYPE_ALL获取所有传感器。

注意:返回与给定类型匹配的唤醒和非唤醒传感器。 检查isWakeUpSensor()以了解返回的Sensor的唤醒属性。

Parameters
type int: of sensors requested
Returns
List<Sensor> a list of sensors matching the asked type.

也可以看看:

getSensors

Added in API level 1
int getSensors ()

此方法在API级别3中已被弃用。
此方法已弃用, getSensorList(int)改为使用getSensorList(int)

Returns
int available sensors.

isDynamicSensorDiscoverySupported

Added in API level 24
boolean isDynamicSensorDiscoverySupported ()

告诉系统是否支持动态传感器发现功能。

Returns
boolean true if dynamic sensor discovery is supported, false otherwise.

registerDynamicSensorCallback

Added in API level 24
void registerDynamicSensorCallback (SensorManager.DynamicSensorCallback callback)

添加DynamicSensorCallback以接收动态传感器连接回调。 重复注册已经注册的回调对象将不会产生额外的效果。

Parameters
callback SensorManager.DynamicSensorCallback: An object that implements the DynamicSensorCallback interface for receiving callbacks.
Throws
IllegalArgumentException when callback is null.

也可以看看:

registerDynamicSensorCallback

Added in API level 24
void registerDynamicSensorCallback (SensorManager.DynamicSensorCallback callback, 
                Handler handler)

添加DynamicSensorCallback以接收动态传感器连接回调。 重复注册已经注册的回调对象将不会产生额外的效果。

Parameters
callback SensorManager.DynamicSensorCallback: An object that implements the DynamicSensorCallback interface for receiving callbacks.
handler Handler: The Handler the sensor connection events will be delivered to.
Throws
IllegalArgumentException when callback is null.

registerListener

Added in API level 3
boolean registerListener (SensorEventListener listener, 
                Sensor sensor, 
                int samplingPeriodUs)

以给定的采样频率为给定的传感器注册一个 SensorEventListener

一旦有事件发生,活动将立即交付给所提供的SensorEventListener 为了降低功耗,应用程序可以使用registerListener(SensorEventListener, Sensor, int, int)并指定正的非零最大报告延迟。

对于非唤醒传感器,事件仅在应用程序处理器(AP)未处于挂起模式时才会发送。 有关更多详细信息,请参阅isWakeUpSensor() 为确保即使在屏幕关闭的情况下也能从非唤醒传感器传送事件,注册到传感器的应用程序必须保持部分唤醒锁定以保持AP唤醒,否则在AP睡着时某些事件可能会丢失。 请注意,尽管AP在休眠时事件可能会丢失,但如果应用程序未明确停用传感器,传感器仍会消耗功率。 应用程序必须在其活动的onPause()方法中注销其SensorEventListener ,以避免设备处于非活动状态时耗电。 有关硬件FIFO(排队)功能和某些传感器事件可能丢失的更多详细信息,请参见registerListener(SensorEventListener, Sensor, int, int)

在唤醒传感器的情况下,传感器产生的每个事件都会导致AP唤醒,确保每个事件都可以传送。 因此,注册到唤醒传感器具有非常显着的功率影响。 致电isWakeUpSensor()检查传感器是否是唤醒传感器。 有关如何减少注册到唤醒传感器的功耗影响的信息,请参阅registerListener(SensorEventListener, Sensor, int, int)

注意:不要将此方法用于TYPE_SIGNIFICANT_MOTION等单触发传感器。 改为使用requestTriggerSensor(TriggerEventListener, Sensor) 使用getReportingMode()获取给定传感器的报告模式。

Parameters
listener SensorEventListener: A SensorEventListener object.
sensor Sensor: The Sensor to register to.
samplingPeriodUs int: The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of the SENSOR_DELAY_* constants.
Returns
boolean true if the sensor is supported and successfully enabled.

也可以看看:

registerListener

Added in API level 19
boolean registerListener (SensorEventListener listener, 
                Sensor sensor, 
                int samplingPeriodUs, 
                int maxReportLatencyUs)

以给定的采样频率和给定的最大报告延迟为给定传感器注册 SensorEventListener

该功能类似于registerListener(SensorEventListener, Sensor, int)但它允许事件在交付之前临时保留在硬件FIFO(队列)中。 这些事件可以存储在硬件FIFO中,最长可达maxReportLatencyUs微秒。 一旦FIFO中的事件之一需要被报告,FIFO中的所有事件都会被顺序报告。 这意味着有些事件会在最大报告延迟时间过去之前报告。

maxReportLatencyUs为0时,该呼叫相当于对 registerListener(SensorEventListener, Sensor, int)的呼叫,因为它要求事件尽快发送。

sensor.maxFifoEventCount()为0时,传感器不使用FIFO,因此该呼叫也将等于 registerListener(SensorEventListener, Sensor, int)

maxReportLatencyUs设置为正值可以减少AP(应用处理器)接收的中断数量,从而降低功耗,因为当传感器捕获数据时,AP可以切换到较低的功耗状态。 当注册唤醒传感器时,这一点尤其重要,每个中断都会导致AP在暂停模式下唤醒。 有关唤醒传感器的更多信息,请参阅isWakeUpSensor()

Note: Don't use this method with one-shot trigger sensors such as TYPE_SIGNIFICANT_MOTION. Use requestTriggerSensor(TriggerEventListener, Sensor) instead.

Parameters
listener SensorEventListener: A SensorEventListener object that will receive the sensor events. If the application is interested in receiving flush complete notifications, it should register with SensorEventListener2 instead.
sensor Sensor: The Sensor to register to.
samplingPeriodUs int: The desired delay between two consecutive events in microseconds. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. Can be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, SENSOR_DELAY_FASTEST or the delay in microseconds.
maxReportLatencyUs int: Maximum time in microseconds that events can be delayed before being reported to the application. A large value allows reducing the power consumption associated with the sensor. If maxReportLatencyUs is set to zero, events are delivered as soon as they are available, which is equivalent to calling registerListener(SensorEventListener, Sensor, int).
Returns
boolean true if the sensor is supported and successfully enabled.

也可以看看:

registerListener

Added in API level 3
boolean registerListener (SensorEventListener listener, 
                Sensor sensor, 
                int samplingPeriodUs, 
                Handler handler)

为给定的传感器注册一个SensorEventListener 一旦可用,事件就会以连续模式传送。 为了降低功耗,应用程序可以使用registerListener(SensorEventListener, Sensor, int, int)并指定正的非零最大报告延迟。

Note: Don't use this method with a one shot trigger sensor such as TYPE_SIGNIFICANT_MOTION. Use requestTriggerSensor(TriggerEventListener, Sensor) instead.

Parameters
listener SensorEventListener: A SensorEventListener object.
sensor Sensor: The Sensor to register to.
samplingPeriodUs int: The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of the SENSOR_DELAY_* constants.
handler Handler: The Handler the sensor events will be delivered to.
Returns
boolean true if the sensor is supported and successfully enabled.

也可以看看:

registerListener

Added in API level 1
boolean registerListener (SensorListener listener, 
                int sensors)

此方法在API级别3中已被弃用。
此方法已弃用, registerListener(SensorEventListener, Sensor, int)改为使用registerListener(SensorEventListener, Sensor, int)

为给定的传感器注册一个监听器。

Parameters
listener SensorListener: sensor listener object
sensors int: a bit masks of the sensors to register to
Returns
boolean true if the sensor is supported and successfully enabled

registerListener

Added in API level 1
boolean registerListener (SensorListener listener, 
                int sensors, 
                int rate)

此方法在API级别3中已被弃用。
此方法已弃用, registerListener(SensorEventListener, Sensor, int)改为使用registerListener(SensorEventListener, Sensor, int)

为给定的传感器注册SensorListener。

Parameters
listener SensorListener: sensor listener object
sensors int: a bit masks of the sensors to register to
rate int: rate of events. This is only a hint to the system. events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST.
Returns
boolean true if the sensor is supported and successfully enabled

registerListener

Added in API level 19
boolean registerListener (SensorEventListener listener, 
                Sensor sensor, 
                int samplingPeriodUs, 
                int maxReportLatencyUs, 
                Handler handler)

以给定的采样频率和给定的最大报告延迟为给定传感器注册 SensorEventListener

Parameters
listener SensorEventListener: A SensorEventListener object that will receive the sensor events. If the application is interested in receiving flush complete notifications, it should register with SensorEventListener2 instead.
sensor Sensor: The Sensor to register to.
samplingPeriodUs int: The desired delay between two consecutive events in microseconds. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. Can be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, SENSOR_DELAY_FASTEST or the delay in microseconds.
maxReportLatencyUs int: Maximum time in microseconds that events can be delayed before being reported to the application. A large value allows reducing the power consumption associated with the sensor. If maxReportLatencyUs is set to zero, events are delivered as soon as they are available, which is equivalent to calling registerListener(SensorEventListener, Sensor, int).
handler Handler: The Handler the sensor events will be delivered to.
Returns
boolean true if the sensor is supported and successfully enabled.

也可以看看:

remapCoordinateSystem

Added in API level 3
boolean remapCoordinateSystem (float[] inR, 
                int X, 
                int Y, 
                float[] outR)

旋转提供的旋转矩阵,以便在不同的坐标系中表示。 当应用程序需要在不同的坐标系统中计算设备的三个方位角时(参见getOrientation(float[], float[]) ),通常会使用这种方法。

当使用旋转矩阵进行绘制时(例如使用OpenGL ES),除非屏幕物理旋转,否则通常不需要使用此功能进行变换,在这种情况下,您可以使用Display.getRotation()来检索当前旋转的屏幕。 请注意,由于用户通常可以自由旋转屏幕,因此在决定使用此参数时通常应考虑轮换。

例子:

  • Using the camera (Y axis along the camera's axis) for an augmented reality application where the rotation angles are needed:
    • remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);

  • Using the device as a mechanical compass when rotation is Surface.ROTATION_90:
    • remapCoordinateSystem(inR, AXIS_Y, AXIS_MINUS_X, outR);

    Beware of the above example. This call is needed only to account for a rotation from its natural orientation when calculating the rotation angles (see getOrientation(float[], float[])). If the rotation matrix is also used for rendering, it may not need to be transformed, for instance if your Activity is running in landscape mode.

由于得到的坐标系是正交的,因此只需要指定两个轴。

Parameters
inR float: the rotation matrix to be transformed. Usually it is the matrix returned by getRotationMatrix(float[], float[], float[], float[]).
X int: defines the axis of the new cooridinate system that coincide with the X axis of the original coordinate system.
Y int: defines the axis of the new cooridinate system that coincide with the Y axis of the original coordinate system.
outR float: the transformed rotation matrix. inR and outR should not be the same array.
Returns
boolean true on success. false if the input parameters are incorrect, for instance if X and Y define the same axis. Or if inR and outR don't have the same length.

也可以看看:

requestTriggerSensor

Added in API level 18
boolean requestTriggerSensor (TriggerEventListener listener, 
                Sensor sensor)

请求接收触发传感器的触发事件。

当传感器检测到触发事件条件(例如TYPE_SIGNIFICANT_MOTION的情况下的重要动作)时,将提供一次触发侦听器,然后其接收触发事件的请求将被取消。 要继续接收触发事件,应用程序必须再次请求接收触发事件。

Parameters
listener TriggerEventListener: The listener on which the onTrigger(TriggerEvent) will be delivered.
sensor Sensor: The sensor to be enabled.
Returns
boolean true if the sensor was successfully enabled.
Throws
IllegalArgumentException when sensor is null or not a trigger sensor.

unregisterDynamicSensorCallback

Added in API level 24
void unregisterDynamicSensorCallback (SensorManager.DynamicSensorCallback callback)

删除 DynamicSensorCallback以停止向该回调发送动态传感器连接事件。

Parameters
callback SensorManager.DynamicSensorCallback: An object that implements the DynamicSensorCallback interface for receiving callbacks.

unregisterListener

Added in API level 3
void unregisterListener (SensorEventListener listener)

取消注册所有传感器的监听器。

Parameters
listener SensorEventListener: a SensorListener object

也可以看看:

unregisterListener

Added in API level 3
void unregisterListener (SensorEventListener listener, 
                Sensor sensor)

取消注册与其注册的传感器的监听器。

Note: Don't use this method with a one shot trigger sensor such as TYPE_SIGNIFICANT_MOTION. Use cancelTriggerSensor(TriggerEventListener, Sensor) instead.

Parameters
listener SensorEventListener: a SensorEventListener object
sensor Sensor: the sensor to unregister from

也可以看看:

unregisterListener

Added in API level 1
void unregisterListener (SensorListener listener)

此方法在API级别3中已被弃用。
此方法已弃用, unregisterListener(SensorEventListener)改为使用unregisterListener(SensorEventListener)

取消注册所有传感器的监听器。

Parameters
listener SensorListener: a SensorListener object

unregisterListener

Added in API level 1
void unregisterListener (SensorListener listener, 
                int sensors)

此方法在API级别3中已被弃用。
此方法已弃用, unregisterListener(SensorEventListener, Sensor)改为使用unregisterListener(SensorEventListener, Sensor)

取消注册与其注册的传感器的监听器。

Parameters
listener SensorListener: a SensorListener object
sensors int: a bit masks of the sensors to unregister from

Hooray!