Android-Environment Sensors
2015-12-19 16:26
429 查看
The Android platform provides four sensors that let you monitor various environmental properties. You can use these sensors to monitor relative ambient humidity, illuminance, ambient
pressure, and ambient temperature near an Android-powered device.
》Using the Light, Pressure, and Temperature Sensors
Then you register a sensor listener in the
incoming sensor data in the
method. The following code shows you how to do this:
》Using the Humidity(湿度) Sensor
You can acquire raw relative humidity data by using the humidity sensor the same way that you use
the light, pressure, and temperature sensors. However, if a device has both a humidity sensor (
and a temperature sensor (
you can use these two data streams to calculate the dew point and the absolute humidity.
pressure, and ambient temperature near an Android-powered device.
》Using the Light, Pressure, and Temperature Sensors
Then you register a sensor listener in the
onResume()method, and start handling
incoming sensor data in the
onSensorChanged()callback
method. The following code shows you how to do this:
public class SensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mPressure; @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. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mPressure = mSensorManager.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 millibars_of_pressure = event.values[0]; // Do something with this sensor data. } @Override protected void onResume() { // Register a listener for the sensor. super.onResume(); mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { // Be sure to unregister the sensor when the activity pauses. super.onPause(); mSensorManager.unregisterListener(this); } }
》Using the Humidity(湿度) Sensor
You can acquire raw relative humidity data by using the humidity sensor the same way that you use
the light, pressure, and temperature sensors. However, if a device has both a humidity sensor (
TYPE_RELATIVE_HUMIDITY)
and a temperature sensor (
TYPE_AMBIENT_TEMPERATURE)
you can use these two data streams to calculate the dew point and the absolute humidity.
相关文章推荐
- Android四大组件之一Service
- android的线程安全
- Android EditText 输入数字和小数,设置输入的范围0.001-1000
- android自定义图标字体的使用
- AndroidStudio中直接下载Github中的代码
- Android Handler的使用示例:结合源码理解Android Handler机制(一)
- Android之Fragment学习(二)
- android项目Tab标签页面大汇总(Fragment、Fragment+ViewPage、TabLayout)
- Android Studio界面菜单翻译
- Android中Service的使用详解和注意点(LocalService)
- 5.1以上系统 #000000layout背景色不生效
- Android应用之PreferenceFragment
- Toolbar使用初探
- Activity触摸事件的分发机制
- Android数据库ORMlite框架详解
- Android性能优化之布局优化总结
- AndroidStudio导入第三方类库
- android中卡号输入框控件(每四位用空格分隔)(解决输入法跳转的问题)
- Android-Position Sensors
- Android之ExpandableListView控件使用