環境感應器
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 平台提供四種感應器,可供你監控各種環境屬性。
你可以使用這些感應器監控相對環境濕度、光線、環境壓力和
周遭環境溫度。這四種環境感應器全都是硬體式感應器
且只有在裝置製造商內建於裝置上時才可使用。除了
光源感應器,大多數裝置製造商用來控制螢幕亮度和環境
部分裝置可能無法隨時使用感應器。因此,請特別注意
在執行階段驗證是否存在環境感應器,再嘗試從
基礎架構
大多數動作感應器和位置感應器會傳回多維感應器陣列,這與大多數的動作感應器和位置感應器不同
每個 SensorEvent
的值,環境感應器都會傳回單一感應器
每個資料事件的價值例如攝氏溫度或 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()
回呼方法的實作。另外,請
請務必在活動暫停時,確實取消註冊感應器。這樣可以防止感應器
會持續感應資料並消耗電池電力
使用濕度感應器
如要取得原始相對濕度資料,請同樣使用濕度感應器
包括光線、壓力和溫度感應器不過,如果裝置同時配備濕度感應器
你可以使用這兩個資料串流 (TYPE_RELATIVE_HUMIDITY
) 和溫度感應器 (TYPE_AMBIENT_TEMPERATURE
) 來計算
露點和絕對濕度
露點
露點是指特定空氣量必須保持不變的溫度
水蒸氣壓,讓水蒸氣凝聚水。以下公式顯示
可以計算露點:
地點
- td = 以攝氏為單位的露點溫度
- t = 實際溫度,以攝氏度為單位
- 相對濕度 (百分比) = 實際相對濕度 (百分比)
- m = 17.62
- n = 243.12
絕對濕度
絕對濕度是指乾燥量中各個水蒸氣的質量。絕對時間
濕度的測量單位為公克/公尺3。以下公式顯示
可以計算絕對濕度:
地點
- dv = 絕對濕度,單位為公克/公尺3
- t = 實際溫度,以攝氏度為單位
- 相對濕度 (百分比) = 實際相對濕度 (百分比)
- m = 17.62
- n = 243.12 度 C
- A = 6.112 hPa
另請參閱
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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)"]]