SensorManager开发参考

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

对SensorManager内容进行摘抄

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.

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

classSensorManager.DynamicSensorCallback

Used for receiving notifications from the SensorManager when dynamic sensors are connected or disconnected. 

Constants

intAXIS_MINUS_X

see remapCoordinateSystem(float, int, int, float)

intAXIS_MINUS_Y

see remapCoordinateSystem(float, int, int, float)

intAXIS_MINUS_Z

see remapCoordinateSystem(float, int, int, float)

intAXIS_X

see remapCoordinateSystem(float, int, int, float)

intAXIS_Y

see remapCoordinateSystem(float, int, int, float)

intAXIS_Z

see remapCoordinateSystem(float, int, int, float)

intDATA_X

This constant was deprecated in API level 15. use Sensor instead.

intDATA_Y

This constant was deprecated in API level 15. use Sensor instead.

intDATA_Z

This constant was deprecated in API level 15. use Sensor instead.

floatGRAVITY_DEATH_STAR_I

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

floatGRAVITY_EARTH

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

floatGRAVITY_JUPITER

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

floatGRAVITY_MARS

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

floatGRAVITY_MERCURY

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

floatGRAVITY_MOON

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

floatGRAVITY_NEPTUNE

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

floatGRAVITY_PLUTO

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

floatGRAVITY_SATURN

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

floatGRAVITY_SUN

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

floatGRAVITY_THE_ISLAND

Gravity on the island

floatGRAVITY_URANUS

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

floatGRAVITY_VENUS

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

floatLIGHT_CLOUDY

luminance under a cloudy sky in lux

floatLIGHT_FULLMOON

luminance at night with full moon in lux

floatLIGHT_NO_MOON

luminance at night with no moon in lux

floatLIGHT_OVERCAST

luminance under an overcast sky in lux

floatLIGHT_SHADE

luminance in shade in lux

floatLIGHT_SUNLIGHT

luminance of sunlight in lux

floatLIGHT_SUNLIGHT_MAX

Maximum luminance of sunlight in lux

floatLIGHT_SUNRISE

luminance at sunrise in lux

floatMAGNETIC_FIELD_EARTH_MAX

Maximum magnetic field on Earth's surface

floatMAGNETIC_FIELD_EARTH_MIN

Minimum magnetic field on Earth's surface

floatPRESSURE_STANDARD_ATMOSPHERE

Standard atmosphere, or average sea-level pressure in hPa (millibar)

intRAW_DATA_INDEX

This constant was deprecated in API level 15. use Sensor instead.

intRAW_DATA_X

This constant was deprecated in API level 15. use Sensor instead.

intRAW_DATA_Y

This constant was deprecated in API level 15. use Sensor instead.

intRAW_DATA_Z

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_ACCELEROMETER

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_ALL

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_DELAY_FASTEST

get sensor data as fast as possible

intSENSOR_DELAY_GAME

rate suitable for games

intSENSOR_DELAY_NORMAL

rate (default) suitable for screen orientation changes

intSENSOR_DELAY_UI

rate suitable for the user interface

intSENSOR_LIGHT

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_MAGNETIC_FIELD

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_MAX

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_MIN

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_ORIENTATION

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_ORIENTATION_RAW

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_PROXIMITY

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_STATUS_ACCURACY_HIGH

This sensor is reporting data with maximum accuracy

intSENSOR_STATUS_ACCURACY_LOW

This sensor is reporting data with low accuracy, calibration with the environment is needed

intSENSOR_STATUS_ACCURACY_MEDIUM

This sensor is reporting data with an average level of accuracy, calibration with the environment may improve the readings

intSENSOR_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).

intSENSOR_STATUS_UNRELIABLE

The values returned by this sensor cannot be trusted, calibration is needed or the environment doesn't allow readings

intSENSOR_TEMPERATURE

This constant was deprecated in API level 15. use Sensor instead.

intSENSOR_TRICORDER

This constant was deprecated in API level 15. use Sensor instead.

floatSTANDARD_GRAVITY

Standard gravity (g) on Earth.

Public methods

booleancancelTriggerSensor(TriggerEventListener listener, Sensor sensor)

Cancels receiving trigger events for a trigger sensor.

SensorDirectChannelcreateDirectChannel(MemoryFile mem)

Create a sensor direct channel backed by shared memory wrapped in MemoryFile object.

SensorDirectChannelcreateDirectChannel(HardwareBuffer mem)

Create a sensor direct channel backed by shared memory wrapped in HardwareBuffer object.

booleanflush(SensorEventListener listener)

Flushes the FIFO of all the sensors registered for this listener.

static floatgetAltitude(float p0, float p)

Computes the Altitude in meters from the atmospheric pressure and the pressure at sea level.

static voidgetAngleChange(float[] angleChange, float[] R, float[] prevR)

Helper function to compute the angle change between two rotation matrices.

SensorgetDefaultSensor(int type)

Use this method to get the default sensor for a given type.

SensorgetDefaultSensor(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 floatgetInclination(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 voidgetQuaternionFromVector(float[] Q, float[] rv)

Helper function to convert a rotation vector to a normalized quaternion.

static booleangetRotationMatrix(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 voidgetRotationMatrixFromVector(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.

intgetSensors()

This method was deprecated in API level 15. This method is deprecated, use SensorManager.getSensorList(int) instead

booleanisDynamicSensorDiscoverySupported()

Tell if dynamic sensor discovery feature is supported by system.

voidregisterDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)

Add a DynamicSensorCallback to receive dynamic sensor connection callbacks.

voidregisterDynamicSensorCallback(SensorManager.DynamicSensorCallback callback, Handler handler)

Add a DynamicSensorCallback to receive dynamic sensor connection callbacks.

booleanregisterListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs)

Registers a SensorEventListener for the given sensor at the given sampling frequency.

booleanregisterListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxReportLatencyUs)

Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.

booleanregisterListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, Handler handler)

Registers a SensorEventListener for the given sensor.

booleanregisterListener(SensorListener listener, int sensors)

This method was deprecated in API level 15. This method is deprecated, use SensorManager.registerListener(SensorEventListener, Sensor, int) instead.

booleanregisterListener(SensorListener listener, int sensors, int rate)

This method was deprecated in API level 15. This method is deprecated, use SensorManager.registerListener(SensorEventListener, Sensor, int) instead.

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

Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.

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

Rotates the supplied rotation matrix so it is expressed in a different coordinate system.

booleanrequestTriggerSensor(TriggerEventListener listener, Sensor sensor)

Requests receiving trigger events for a trigger sensor.

voidunregisterDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)

Remove a DynamicSensorCallback to stop sending dynamic sensor connection events to that callback.

voidunregisterListener(SensorEventListener listener)

Unregisters a listener for all sensors.

voidunregisterListener(SensorEventListener listener, Sensor sensor)

Unregisters a listener for the sensors with which it is registered.

voidunregisterListener(SensorListener listener)

This method was deprecated in API level 15. This method is deprecated, use SensorManager.unregisterListener(SensorEventListener) instead.

voidunregisterListener(SensorListener listener, 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

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

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

public static final int AXIS_MINUS_Z

see remapCoordinateSystem(float, int, int, float)

Constant Value: 131 (0x00000083)

AXIS_X

Added in API level 3

public static final int AXIS_X

see remapCoordinateSystem(float, int, int, float)

Constant Value: 1 (0x00000001)

AXIS_Y

Added in API level 3

public static final int AXIS_Y

see remapCoordinateSystem(float, int, int, float)

Constant Value: 2 (0x00000002)

AXIS_Z

Added in API level 3

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

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

SensorListener.onSensorChanged(int, float)

Constant Value: 0 (0x00000000)

DATA_Y

Added in API level 1
Deprecated in API level 15

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

SensorListener.onSensorChanged(int, float)

Constant Value: 1 (0x00000001)

DATA_Z

Added in API level 1
Deprecated in API level 15

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

SensorListener.onSensorChanged(int, float)

Constant Value: 2 (0x00000002)

GRAVITY_DEATH_STAR_I

Added in API level 1

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

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

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

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

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

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

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

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

public static final float GRAVITY_SATURN

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

Constant Value: 8.96

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫特踏实

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值