เซ็นเซอร์สภาพแวดล้อม
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แพลตฟอร์ม Android มีเซ็นเซอร์ 4 ตัวที่ช่วยให้คุณตรวจสอบคุณสมบัติด้านสิ่งแวดล้อมที่หลากหลายได้
คุณสามารถใช้เซ็นเซอร์เหล่านี้เพื่อตรวจดูความชื้นสัมพัทธ์ ความสว่าง ความดันบรรยากาศ และ
อุณหภูมิแวดล้อมใกล้กับอุปกรณ์ที่ใช้ Android เซ็นเซอร์ตรวจจับสภาพแวดล้อมทั้ง 4 ตัวทำงานด้วยฮาร์ดแวร์
และพร้อมใช้งานก็ต่อเมื่อผู้ผลิตอุปกรณ์ได้ผสานรวมอุปกรณ์ไว้ ยกเว้นในกรณี
เซ็นเซอร์แสง ซึ่งผู้ผลิตอุปกรณ์ส่วนใหญ่ใช้เพื่อควบคุมความสว่างและสภาพแวดล้อมของหน้าจอ
เซ็นเซอร์อาจไม่พร้อมใช้งานในอุปกรณ์เสมอไป ด้วยเหตุนี้ คุณจึงจำเป็นต้อง
ตรวจสอบขณะรันไทม์ว่ามีเซ็นเซอร์สภาพแวดล้อมหรือไม่ก่อนที่คุณจะพยายามดึงข้อมูลจาก
ได้
ต่างจากเซ็นเซอร์ตรวจจับการเคลื่อนไหวและเซ็นเซอร์ตำแหน่งส่วนใหญ่ ซึ่งแสดงผลเซ็นเซอร์แบบหลายมิติ
ค่าสำหรับ SensorEvent
แต่ละรายการ เซ็นเซอร์สภาพแวดล้อมจะแสดงเซ็นเซอร์เดียว
สําหรับเหตุการณ์ข้อมูลแต่ละเหตุการณ์ เช่น อุณหภูมิในหน่วย°C หรือความดันในหน่วย hPa
นอกจากนี้ ต่างจากเซ็นเซอร์ตรวจจับการเคลื่อนไหวและเซ็นเซอร์ตำแหน่ง ซึ่งมักต้องใช้ความถี่สูงหรือต่ำ
ในการกรองข้อมูลทั่วไป เซ็นเซอร์ตรวจจับสภาพแวดล้อมจึงไม่จําเป็นต้องกรองข้อมูลหรือประมวลผลข้อมูล ตาราง
1 จะให้ข้อมูลสรุปเกี่ยวกับเซ็นเซอร์สภาพแวดล้อมที่แพลตฟอร์ม Android รองรับ
ตาราง 1 เซ็นเซอร์สภาพแวดล้อมที่แพลตฟอร์ม Android รองรับ
1 การใช้งานจะแตกต่างกันไปในแต่ละอุปกรณ์
อุปกรณ์ เซ็นเซอร์นี้เลิกใช้งานแล้วใน Android 4.0 (API ระดับ 14)
ใช้เซ็นเซอร์แสง ความดัน และอุณหภูมิ
โดยปกติแล้วข้อมูลดิบที่ได้มาจากเซ็นเซอร์แสง ความดัน และอุณหภูมิไม่จำเป็นต้องใช้
การปรับเทียบ การกรอง หรือการดัดแปลง ซึ่งทำให้สิ่งเหล่านี้เป็นเซ็นเซอร์ที่ใช้งานง่ายที่สุด ถึง
ได้ข้อมูลจากเซ็นเซอร์เหล่านี้ ซึ่งเป็นการสร้างอินสแตนซ์ของคลาส SensorManager
ซึ่งสามารถใช้เพื่อรับอินสแตนซ์ของเซ็นเซอร์ทางกายภาพได้
จากนั้นลงทะเบียน Listener เซ็นเซอร์ในเมธอด onResume()
และเริ่มจัดการข้อมูลเซ็นเซอร์ที่เข้ามาในเมธอด Callback 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);
}
}
คุณต้องมีการใช้งานทั้งเมธอด Callback onAccuracyChanged()
และ onSensorChanged()
เสมอ นอกจากนี้
อย่าลืมยกเลิกการลงทะเบียนเซ็นเซอร์ทุกครั้งเมื่อกิจกรรมหยุดลงชั่วคราว เพื่อป้องกันไม่ให้เซ็นเซอร์
ตรวจจับข้อมูลอย่างต่อเนื่องและทำให้แบตเตอรี่หมดเร็ว
ใช้เซ็นเซอร์วัดความชื้น
คุณดูข้อมูลความชื้นสัมพัทธ์แบบดิบได้โดยใช้เซ็นเซอร์วัดความชื้นวิธีเดียวกันกับที่ใช้
เซ็นเซอร์แสง ความดัน และอุณหภูมิ แต่หากอุปกรณ์มีทั้งเซ็นเซอร์ความชื้น
(TYPE_RELATIVE_HUMIDITY
) และเซ็นเซอร์อุณหภูมิ (TYPE_AMBIENT_TEMPERATURE
) คุณสามารถใช้สตรีมข้อมูลทั้งสองนี้ในการคำนวณได้
จุดน้ำค้างและความชื้นสัมบูรณ์
จุดน้ำค้าง
จุดน้ำค้างคืออุณหภูมิที่ต้องทำความเย็นในปริมาณที่กำหนดอย่างต่อเนื่อง
ความดันของบรรยากาศ สำหรับไอน้ำที่จะควบแน่นลงในน้ำ สมการต่อไปนี้จะแสดงวิธีที่คุณ
สามารถคำนวณจุดน้ำค้างได้
ที่ไหน
- td = อุณหภูมิจุดน้ำค้างเป็นองศา C
- t = อุณหภูมิจริงเป็นองศา C
- RH = ความชื้นสัมพัทธ์ตามจริงในหน่วยเปอร์เซ็นต์ (%)
- ม. = 17.62
- = 243.12
ความชื้นสัมบูรณ์
ความชื้นสัมบูรณ์คือมวลไอน้ำในปริมาณอากาศแห้งที่ระบุ สัมบูรณ์
ความชื้นวัดเป็นหน่วยกรัม/เมตร3 สมการต่อไปนี้จะแสดงวิธีที่คุณ
สามารถคำนวณความชื้นสัมบูรณ์ได้
ที่ไหน
- dv = ความชื้นสัมบูรณ์เป็นกรัม/เมตร3
- t = อุณหภูมิจริงเป็นองศา C
- RH = ความชื้นสัมพัทธ์ตามจริงในหน่วยเปอร์เซ็นต์ (%)
- ม. = 17.62
- Tn = 243.12 องศาC
- A = 6.112 hPa
คุณควรอ่าน
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],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)"]]