환경 센서
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Android 플랫폼은 다양한 환경 속성을 모니터링할 수 있는 센서 4개를 제공합니다.
이러한 센서를 사용하여 상대 주변 습도, 조도, 주변 압력 및
80도. 4개의 환경 센서는 모두 하드웨어 기반입니다
기기 제조업체에서 기기에 내장한 경우에만 사용할 수 있습니다. 단,
대부분의 기기 제조업체가 화면 밝기, 주변 환경을 제어하는 데 사용하는 광센서
기기에서 센서를 항상 사용할 수 있는 것은 아닙니다. 그렇기 때문에
환경 센서의 존재 여부를 런타임에 확인(
있습니다.
센서의 다차원 배열을 반환하는 대부분의 움직임 감지 센서 및 위치 센서와 달리
값이 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)
}
}
자바
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 = 이슬점 온도(˚C)
- t = 실제 온도(˚C)
- RH = 실제 상대 습도(%)
- m = 17.62
- Tn = 243.12
절대 습도
절대 습도는 주어진 양의 건조 공기에 포함된 수증기의 질량입니다. 절대적
습도는 그램/미터3로 측정됩니다. 다음 식은
절대 습도를 계산할 수 있습니다.
여기서
- dv = 절대 습도(그램/미터3)
- t = 실제 온도(˚C)
- RH = 실제 상대 습도(%)
- m = 17.62
- Tn = 243.12˚C
- A = 6.112 hPa
참고 사항
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-26(UTC)
[null,null,["최종 업데이트: 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)"]]