SensorManager开发参考

参考网址:https://developer.android.com/reference/android/hardware/SensorManager

对SensorManager内容进行摘抄

java 复制代码
public abstract class SensorManager
extends Object

|---|--------------------------------|
| java.lang.Object ||
| ↳ | android.hardware.SensorManager |


SensorManager lets you access the device's sensors.

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. Sensor.TYPE_SIGNIFICANT_MOTION is an example of a trigger sensor.

In order to access sensor data at high sampling rates (i.e. greater than 200 Hz for SensorEventListener and greater than SensorDirectChannel.RATE_NORMAL for SensorDirectChannel), apps must declare the Manifest.permission.HIGH_SAMPLING_RATE_SENSORS permission in their AndroidManifest.xml file.

java 复制代码
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:

Summary

#### Nested classes
class SensorManager.DynamicSensorCallback Used for receiving notifications from the SensorManager when dynamic sensors are connected or disconnected.
#### Constants
int AXIS_MINUS_X see remapCoordinateSystem(float, int, int, float)
int AXIS_MINUS_Y see remapCoordinateSystem(float, int, int, float)
int AXIS_MINUS_Z see remapCoordinateSystem(float, int, int, float)
int AXIS_X see remapCoordinateSystem(float, int, int, float)
int AXIS_Y see remapCoordinateSystem(float, int, int, float)
int AXIS_Z see remapCoordinateSystem(float, int, int, float)
int DATA_X This constant was deprecated in API level 15. use Sensor instead.
int DATA_Y This constant was deprecated in API level 15. use Sensor instead.
int DATA_Z This constant was deprecated in API level 15. use Sensor instead.
float GRAVITY_DEATH_STAR_I Gravity (estimate) on the first Death Star in Empire units (m/s^2)
float GRAVITY_EARTH Earth's gravity in SI units (m/s^2)
float GRAVITY_JUPITER Jupiter's gravity in SI units (m/s^2)
float GRAVITY_MARS Mars' gravity in SI units (m/s^2)
float GRAVITY_MERCURY Mercury's gravity in SI units (m/s^2)
float GRAVITY_MOON The Moon's gravity in SI units (m/s^2)
float GRAVITY_NEPTUNE Neptune's gravity in SI units (m/s^2)
float GRAVITY_PLUTO Pluto's gravity in SI units (m/s^2)
float GRAVITY_SATURN Saturn's gravity in SI units (m/s^2)
float GRAVITY_SUN Sun's gravity in SI units (m/s^2)
float GRAVITY_THE_ISLAND Gravity on the island
float GRAVITY_URANUS Uranus' gravity in SI units (m/s^2)
float GRAVITY_VENUS Venus' gravity in SI units (m/s^2)
float LIGHT_CLOUDY luminance under a cloudy sky in lux
float LIGHT_FULLMOON luminance at night with full moon in lux
float LIGHT_NO_MOON luminance at night with no moon in lux
float LIGHT_OVERCAST luminance under an overcast sky in lux
float LIGHT_SHADE luminance in shade in lux
float LIGHT_SUNLIGHT luminance of sunlight in lux
float LIGHT_SUNLIGHT_MAX Maximum luminance of sunlight in lux
float LIGHT_SUNRISE luminance at sunrise in lux
float MAGNETIC_FIELD_EARTH_MAX Maximum magnetic field on Earth's surface
float MAGNETIC_FIELD_EARTH_MIN Minimum magnetic field on Earth's surface
float PRESSURE_STANDARD_ATMOSPHERE Standard atmosphere, or average sea-level pressure in hPa (millibar)
int RAW_DATA_INDEX This constant was deprecated in API level 15. use Sensor instead.
int RAW_DATA_X This constant was deprecated in API level 15. use Sensor instead.
int RAW_DATA_Y This constant was deprecated in API level 15. use Sensor instead.
int RAW_DATA_Z This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_ACCELEROMETER This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_ALL This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_DELAY_FASTEST get sensor data as fast as possible
int SENSOR_DELAY_GAME rate suitable for games
int SENSOR_DELAY_NORMAL rate (default) suitable for screen orientation changes
int SENSOR_DELAY_UI rate suitable for the user interface
int SENSOR_LIGHT This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_MAGNETIC_FIELD This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_MAX This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_MIN This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_ORIENTATION This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_ORIENTATION_RAW This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_PROXIMITY This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_STATUS_ACCURACY_HIGH This sensor is reporting data with maximum accuracy
int SENSOR_STATUS_ACCURACY_LOW This sensor is reporting data with low accuracy, calibration with the environment is needed
int SENSOR_STATUS_ACCURACY_MEDIUM This sensor is reporting data with an average level of accuracy, calibration with the environment may improve the readings
int SENSOR_STATUS_NO_CONTACT The values returned by this sensor cannot be trusted because the sensor had no contact with what it was measuring (for example, the heart rate monitor is not in contact with the user).
int SENSOR_STATUS_UNRELIABLE The values returned by this sensor cannot be trusted, calibration is needed or the environment doesn't allow readings
int SENSOR_TEMPERATURE This constant was deprecated in API level 15. use Sensor instead.
int SENSOR_TRICORDER This constant was deprecated in API level 15. use Sensor instead.
float STANDARD_GRAVITY Standard gravity (g) on Earth.
#### Public methods
boolean cancelTriggerSensor(TriggerEventListenerlistener,Sensorsensor) Cancels receiving trigger events for a trigger sensor.
SensorDirectChannel createDirectChannel(MemoryFilemem) Create a sensor direct channel backed by shared memory wrapped in MemoryFile object.
SensorDirectChannel createDirectChannel(HardwareBuffermem) Create a sensor direct channel backed by shared memory wrapped in HardwareBuffer object.
boolean flush(SensorEventListenerlistener) Flushes the FIFO of all the sensors registered for this listener.
static float getAltitude(float p0, float p) Computes the Altitude in meters from the atmospheric pressure and the pressure at sea level.
static void getAngleChange(float[] angleChange, float[] R, float[] prevR) Helper function to compute the angle change between two rotation matrices.
Sensor getDefaultSensor(int type) Use this method to get the default sensor for a given type.
Sensor getDefaultSensor(int type, boolean wakeUp) Return a Sensor with the given type and wakeUp properties.
List<Sensor> getDynamicSensorList(int type) Use this method to get a list of available dynamic sensors of a certain type.
static float getInclination(float[] I) Computes the geomagnetic inclination angle in radians from the inclination matrix I returned by getRotationMatrix(float, float, float, float).
static float[] getOrientation(float[] R, float[] values) Computes the device's orientation based on the rotation matrix.
static void getQuaternionFromVector(float[] Q, float[] rv) Helper function to convert a rotation vector to a normalized quaternion.
static boolean getRotationMatrix(float[] R, float[] I, float[] gravity, float[] geomagnetic) Computes the inclination matrix I as well as the rotation matrix R transforming a vector from the device coordinate system to the world's coordinate system which is defined as a direct orthonormal basis, where: * 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 function to convert a rotation vector to a rotation matrix.
List<Sensor> getSensorList(int type) Use this method to get the list of available sensors of a certain type.
int getSensors() This method was deprecated in API level 15. This method is deprecated, use SensorManager.getSensorList(int) instead
boolean isDynamicSensorDiscoverySupported() Tell if dynamic sensor discovery feature is supported by system.
void registerDynamicSensorCallback(SensorManager.DynamicSensorCallbackcallback) Add a DynamicSensorCallback to receive dynamic sensor connection callbacks.
void registerDynamicSensorCallback(SensorManager.DynamicSensorCallbackcallback,Handlerhandler) Add a DynamicSensorCallback to receive dynamic sensor connection callbacks.
boolean registerListener(SensorEventListenerlistener,Sensorsensor, int samplingPeriodUs) Registers a SensorEventListener for the given sensor at the given sampling frequency.
boolean registerListener(SensorEventListenerlistener,Sensorsensor, int samplingPeriodUs, int maxReportLatencyUs) Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.
boolean registerListener(SensorEventListenerlistener,Sensorsensor, int samplingPeriodUs,Handlerhandler) Registers a SensorEventListener for the given sensor.
boolean registerListener(SensorListenerlistener, int sensors) This method was deprecated in API level 15. This method is deprecated, use SensorManager.registerListener(SensorEventListener, Sensor, int) instead.
boolean registerListener(SensorListenerlistener, int sensors, int rate) This method was deprecated in API level 15. This method is deprecated, use SensorManager.registerListener(SensorEventListener, Sensor, int) instead.
boolean registerListener(SensorEventListenerlistener,Sensorsensor, int samplingPeriodUs, int maxReportLatencyUs,Handlerhandler) Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.
static boolean remapCoordinateSystem(float[] inR, int X, int Y, float[] outR) Rotates the supplied rotation matrix so it is expressed in a different coordinate system.
boolean requestTriggerSensor(TriggerEventListenerlistener,Sensorsensor) Requests receiving trigger events for a trigger sensor.
void unregisterDynamicSensorCallback(SensorManager.DynamicSensorCallbackcallback) Remove a DynamicSensorCallback to stop sending dynamic sensor connection events to that callback.
void unregisterListener(SensorEventListenerlistener) Unregisters a listener for all sensors.
void unregisterListener(SensorEventListenerlistener,Sensorsensor) Unregisters a listener for the sensors with which it is registered.
void unregisterListener(SensorListenerlistener) This method was deprecated in API level 15. This method is deprecated, use SensorManager.unregisterListener(SensorEventListener) instead.
void unregisterListener(SensorListenerlistener, int sensors) This method was deprecated in API level 15. This method is deprecated, use SensorManager.unregisterListener(SensorEventListener, Sensor) instead.

| #### Inherited methods |
|------------------------|---|
| ||

Constants

AXIS_MINUS_X

Added in API level 3

java 复制代码
public static final int AXIS_MINUS_X

see remapCoordinateSystem(float, int, int, float)

Constant Value: 129 (0x00000081)

AXIS_MINUS_Y

Added in API level 3

java 复制代码
public static final int AXIS_MINUS_Y

see remapCoordinateSystem(float, int, int, float)

Constant Value: 130 (0x00000082)

AXIS_MINUS_Z

Added in API level 3

java 复制代码
public static final int AXIS_MINUS_Z

see remapCoordinateSystem(float, int, int, float)

Constant Value: 131 (0x00000083)

AXIS_X

Added in API level 3

java 复制代码
public static final int AXIS_X

see remapCoordinateSystem(float, int, int, float)

Constant Value: 1 (0x00000001)

AXIS_Y

Added in API level 3

java 复制代码
public static final int AXIS_Y

see remapCoordinateSystem(float, int, int, float)

Constant Value: 2 (0x00000002)

AXIS_Z

Added in API level 3

java 复制代码
public static final int AXIS_Z

see remapCoordinateSystem(float, int, int, float)

Constant Value: 3 (0x00000003)

DATA_X

Added in API level 1

Deprecated in API level 15

java 复制代码
public static final int DATA_X

This constant was deprecated in API level 15.

use Sensor instead.

Index of the X value in the array returned by

java 复制代码
SensorListener.onSensorChanged(int, float)

Constant Value: 0 (0x00000000)

DATA_Y

Added in API level 1

Deprecated in API level 15

java 复制代码
public static final int DATA_Y

This constant was deprecated in API level 15.

use Sensor instead.

Index of the Y value in the array returned by

java 复制代码
SensorListener.onSensorChanged(int, float)

Constant Value: 1 (0x00000001)

DATA_Z

Added in API level 1

Deprecated in API level 15

java 复制代码
public static final int DATA_Z

This constant was deprecated in API level 15.

use Sensor instead.

Index of the Z value in the array returned by

java 复制代码
SensorListener.onSensorChanged(int, float)

Constant Value: 2 (0x00000002)

GRAVITY_DEATH_STAR_I

Added in API level 1

java 复制代码
public static final float GRAVITY_DEATH_STAR_I

Gravity (estimate) on the first Death Star in Empire units (m/s^2)

Constant Value: 3.5303614E-7

GRAVITY_EARTH

Added in API level 1

java 复制代码
public static final float GRAVITY_EARTH

Earth's gravity in SI units (m/s^2)

Constant Value: 9.80665

GRAVITY_JUPITER

Added in API level 1

java 复制代码
public static final float GRAVITY_JUPITER

Jupiter's gravity in SI units (m/s^2)

Constant Value: 23.12

GRAVITY_MARS

Added in API level 1

java 复制代码
public static final float GRAVITY_MARS

Mars' gravity in SI units (m/s^2)

Constant Value: 3.71

GRAVITY_MERCURY

Added in API level 1

java 复制代码
public static final float GRAVITY_MERCURY

Mercury's gravity in SI units (m/s^2)

Constant Value: 3.7

GRAVITY_MOON

Added in API level 1

java 复制代码
public static final float GRAVITY_MOON

The Moon's gravity in SI units (m/s^2)

Constant Value: 1.6

GRAVITY_NEPTUNE

Added in API level 1

java 复制代码
public static final float GRAVITY_NEPTUNE

Neptune's gravity in SI units (m/s^2)

Constant Value: 11.0

GRAVITY_PLUTO

Added in API level 1

java 复制代码
public static final float GRAVITY_PLUTO

Pluto's gravity in SI units (m/s^2)

Constant Value: 0.6

GRAVITY_SATURN

Added in API level 1

java 复制代码
public static final float GRAVITY_SATURN

Saturn's gravity in SI units (m/s^2)

Constant Value: 8.96

相关推荐
练习本1 小时前
Android系统架构模式分析
android·java·架构·系统架构
每次的天空6 小时前
Kotlin 内联函数深度解析:从源码到实践优化
android·开发语言·kotlin
练习本6 小时前
Android MVC架构的现代化改造:构建清晰单向数据流
android·架构·mvc
早上好啊! 树哥7 小时前
android studio开发:设置屏幕朝向为竖屏,强制应用的包体始终以竖屏(纵向)展示
android·ide·android studio
YY_pdd7 小时前
使用go开发安卓程序
android·golang
Android 小码峰啊9 小时前
Android Compose 框架物理动画之捕捉动画深入剖析(29)
android·spring
bubiyoushang8889 小时前
深入探索Laravel框架中的Blade模板引擎
android·android studio·laravel
cyy2989 小时前
android 记录应用内存
android·linux·运维
CYRUS STUDIO10 小时前
adb 实用命令汇总
android·adb·命令模式·工具
这儿有一堆花10 小时前
安卓应用卡顿、性能低下的背后原因
android·安卓