环境传感器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 平台提供了四种传感器,用于监控各种环境属性。
您可以使用这些传感器来监控相对环境湿度、照度、环境压力和
Android 设备附近的环境温度。所有四个环境传感器均基于硬件
并且只有当设备制造商内置到设备中时才可用。除了
光传感器,大多数设备制造商都使用它来控制屏幕亮度和环境
传感器并不总是在设备上可用。因此,制定一个
在您尝试从 获取数据之前,在运行时验证是否存在环境传感器
。
与大多数移动传感器和位置传感器不同,后两者会返回一个多维传感器数组
每个 SensorEvent
的值,环境传感器会返回一个传感器
每个数据事件的价值例如,以°C 为单位的温度或以 hPa 为单位的压力。
此外,与移动传感器和位置传感器不同,移动传感器和位置传感器通常需要高通或低通
过滤,环境传感器通常不需要任何数据过滤或数据处理。桌子
1 提供了 Android 平台支持的环境传感器的摘要。
表 1. Android 平台支持的环境传感器。
1 实现方式因设备而异
设备。此传感器已在 Android 4.0(API 级别 14)中弃用。
使用光、压力和温度传感器
从光、压力和温度传感器获取的原始数据通常不需要
校准、滤波或修改,这使得它们成为一些最容易使用的传感器。接收者
要从这些传感器中获取数据,您首先需要创建 SensorManager
类的实例,该实例可用于获取物理传感器的实例。
然后,在 onResume()
方法中注册传感器监听器,并开始在 onSensorChanged()
回调方法中处理传入的传感器数据。通过
以下代码展示了如何执行此操作:
Kotlin
class SensorActivity : Activity(), SensorEventListener {
private lateinit var sensorManager: SensorManager
private var pressure: Sensor? = null
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
// Get an instance of the sensor service, and use that to get an instance of
// a particular sensor.
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
pressure = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE)
}
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {
// Do something here if sensor accuracy changes.
}
override fun onSensorChanged(event: SensorEvent) {
val millibarsOfPressure = event.values[0]
// Do something with this sensor data.
}
override fun onResume() {
// Register a listener for the sensor.
super.onResume()
sensorManager.registerListener(this, pressure, SensorManager.SENSOR_DELAY_NORMAL)
}
override fun onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause()
sensorManager.unregisterListener(this)
}
}
Java
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor pressure;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get an instance of the sensor service, and use that to get an instance of
// a particular sensor.
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
pressure = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
float millibarsOfPressure = event.values[0];
// Do something with this sensor data.
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
sensorManager.registerListener(this, pressure, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
sensorManager.unregisterListener(this);
}
}
您必须始终包含 onAccuracyChanged()
和 onSensorChanged()
两种回调方法的实现。此外,
确保在 activity 暂停时始终取消注册传感器。这可以防止传感器
持续检测数据并消耗电池电量。
使用湿度传感器
您可以像使用湿度传感器一样使用湿度传感器来获取原始相对湿度数据
光传感器、压力传感器和温度传感器。不过,如果设备同时具有湿度传感器
(TYPE_RELATIVE_HUMIDITY
) 和温度传感器 (TYPE_AMBIENT_TEMPERATURE
),您可以使用这两个数据流来计算
露点和绝对湿度
露点
露点是给定体积的空气必须在恒定恒定条件下必须冷却的温度
气压,使水蒸气凝结成水。以下等式显示了
可计算露点:
其中:
- td = 以摄氏度为单位的露点温度
- t = 以摄氏度为单位的实际温度
- RH = 以百分比 (%) 表示的实际相对湿度
- m = 17.62
- Tn = 243.12
绝对湿度
绝对湿度是一定体积的干燥空气中的水蒸气质量。绝对
湿度以克/米3 为单位。以下等式显示了
可计算绝对湿度:
其中:
- dv = 以克/米3 为单位的绝对湿度
- t = 以摄氏度为单位的实际温度
- RH = 以百分比 (%) 表示的实际相对湿度
- m = 17.62
- Tn = 243.12 摄氏度
- A = 6.112 hPa
另请阅读
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Environment sensors\n\nThe Android platform provides four sensors that let you monitor various environmental properties.\nYou can use these sensors to monitor relative ambient humidity, illuminance, ambient pressure, and\nambient temperature near an Android-powered device. All four environment sensors are hardware-based\nand are available only if a device manufacturer has built them into a device. With the exception of\nthe light sensor, which most device manufacturers use to control screen brightness, environment\nsensors are not always available on devices. Because of this, it's particularly important that you\nverify at runtime whether an environment sensor exists before you attempt to acquire data from\nit.\n\nUnlike most motion sensors and position sensors, which return a multi-dimensional array of sensor\nvalues for each [SensorEvent](/reference/android/hardware/SensorEvent), environment sensors return a single sensor\nvalue for each data event. For example, the temperature in °C or the pressure in hPa.\nAlso, unlike motion sensors and position sensors, which often require high-pass or low-pass\nfiltering, environment sensors do not typically require any data filtering or data processing. Table\n1 provides a summary of the environment sensors that are supported on the Android platform.\n\n\n**Table 1.** Environment sensors that are supported on the Android platform.\n\n| Sensor | Sensor event data | Units of measure | Data description |\n|-----------------------------------------------------------------------------------------|-------------------|------------------|----------------------------|\n| [TYPE_AMBIENT_TEMPERATURE](/reference/android/hardware/Sensor#TYPE_AMBIENT_TEMPERATURE) | `event.values[0]` | °C | Ambient air temperature. |\n| [TYPE_LIGHT](/reference/android/hardware/Sensor#TYPE_LIGHT) | `event.values[0]` | lx | Illuminance. |\n| [TYPE_PRESSURE](/reference/android/hardware/Sensor#TYPE_PRESSURE) | `event.values[0]` | hPa or mbar | Ambient air pressure. |\n| [TYPE_RELATIVE_HUMIDITY](/reference/android/hardware/Sensor#TYPE_RELATIVE_HUMIDITY) | `event.values[0]` | % | Ambient relative humidity. |\n| [TYPE_TEMPERATURE](/reference/android/hardware/Sensor#TYPE_TEMPERATURE) | `event.values[0]` | °C | Device temperature.^1^ |\n\n^**1**^ Implementations vary from device to\ndevice. This sensor was deprecated in Android 4.0 (API Level 14).\n\nUse the light, pressure, and temperature sensors\n------------------------------------------------\n\nThe raw data you acquire from the light, pressure, and temperature sensors usually requires no\ncalibration, filtering, or modification, which makes them some of the easiest sensors to use. To\nacquire data from these sensors you first create an instance of the [SensorManager](/reference/android/hardware/SensorManager) class, which you can use to get an instance of a physical sensor.\nThen you register a sensor listener in the [onResume()](/reference/android/app/Activity#onResume()) method, and start handling incoming sensor data in the [onSensorChanged()](/reference/android/hardware/SensorEventListener#onSensorChanged(android.hardware.SensorEvent)) callback method. The\nfollowing code shows you how to do this: \n\n### Kotlin\n\n```kotlin\nclass SensorActivity : Activity(), SensorEventListener {\n\n private lateinit var sensorManager: SensorManager\n private var pressure: Sensor? = null\n\n public override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.main)\n\n // Get an instance of the sensor service, and use that to get an instance of\n // a particular sensor.\n sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager\n pressure = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE)\n }\n\n override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {\n // Do something here if sensor accuracy changes.\n }\n\n override fun onSensorChanged(event: SensorEvent) {\n val millibarsOfPressure = event.values[0]\n // Do something with this sensor data.\n }\n\n override fun onResume() {\n // Register a listener for the sensor.\n super.onResume()\n sensorManager.registerListener(this, pressure, SensorManager.SENSOR_DELAY_NORMAL)\n }\n\n override fun onPause() {\n // Be sure to unregister the sensor when the activity pauses.\n super.onPause()\n sensorManager.unregisterListener(this)\n }\n}\n```\n\n### Java\n\n```java\npublic class SensorActivity extends Activity implements SensorEventListener {\n private SensorManager sensorManager;\n private Sensor pressure;\n\n @Override\n public final void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.main);\n\n // Get an instance of the sensor service, and use that to get an instance of\n // a particular sensor.\n sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);\n pressure = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);\n }\n\n @Override\n public final void onAccuracyChanged(Sensor sensor, int accuracy) {\n // Do something here if sensor accuracy changes.\n }\n\n @Override\n public final void onSensorChanged(SensorEvent event) {\n float millibarsOfPressure = event.values[0];\n // Do something with this sensor data.\n }\n\n @Override\n protected void onResume() {\n // Register a listener for the sensor.\n super.onResume();\n sensorManager.registerListener(this, pressure, SensorManager.SENSOR_DELAY_NORMAL);\n }\n\n @Override\n protected void onPause() {\n // Be sure to unregister the sensor when the activity pauses.\n super.onPause();\n sensorManager.unregisterListener(this);\n }\n}\n```\n\nYou must always include implementations of both the [onAccuracyChanged()](/reference/android/hardware/SensorEventListener#onAccuracyChanged(android.hardware.Sensor, int)) and [onSensorChanged()](/reference/android/hardware/SensorEventListener#onSensorChanged(android.hardware.SensorEvent)) callback methods. Also, be\nsure that you always unregister a sensor when an activity pauses. This prevents a sensor from\ncontinually sensing data and draining the battery.\n\nUse the humidity sensor\n-----------------------\n\nYou can acquire raw relative humidity data by using the humidity sensor the same way that you use\nthe light, pressure, and temperature sensors. However, if a device has both a humidity sensor\n([TYPE_RELATIVE_HUMIDITY](/reference/android/hardware/Sensor#TYPE_RELATIVE_HUMIDITY)) and a temperature sensor ([TYPE_AMBIENT_TEMPERATURE](/reference/android/hardware/Sensor#TYPE_AMBIENT_TEMPERATURE)) you can use these two data streams to calculate\nthe dew point and the absolute humidity.\n\n#### Dew point\n\nThe dew point is the temperature at which a given volume of air must be cooled, at constant\nbarometric pressure, for water vapor to condense into water. The following equation shows how you\ncan calculate the dew point:\n\nWhere,\n\n- t~d~ = dew point temperature in degrees C\n- t = actual temperature in degrees C\n- RH = actual relative humidity in percent (%)\n- m = 17.62\n- T~n~ = 243.12\n\n#### Absolute humidity\n\nThe absolute humidity is the mass of water vapor in a given volume of dry air. Absolute\nhumidity is measured in grams/meter^3^. The following equation shows how you\ncan calculate the absolute humidity:\n\nWhere,\n\n- d~v~ = absolute humidity in grams/meter^3^\n- t = actual temperature in degrees C\n- RH = actual relative humidity in percent (%)\n- m = 17.62\n- T~n~ = 243.12 degrees C\n- A = 6.112 hPa\n\n### You should also read\n\n- [Sensors](/guide/topics/sensors)\n- [Sensors Overview](/guide/topics/sensors/sensors_overview)\n- [Position Sensors](/guide/topics/sensors/sensors_position)\n- [Motion Sensors](/guide/topics/sensors/sensors_motion)"]]