使用android传感器自己编写温度计,湿度计 支持android5.0

  编写温湿度计的原理很简单,就是获取温度,湿度传感器的数据,和其它的传感器的使用方法是一样的(前提是手机要有温度,湿度传感器)

  首先是获取传感器管理器:

  (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  可以通过下面这段代码查看手机上有哪些传感器:

		List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
		for (Sensor sensor : deviceSensors) {
			Log.i("sensor", "------------------");
			Log.i("sensor", sensor.getName());
			Log.i("sensor", sensor.getVendor());
			Log.i("sensor", Integer.toString(sensor.getType()));
			Log.i("sensor", "------------------");
		}
  接下来就是获取温度传感器和湿度传感器:

  mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); //温度传感器
  mSensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);  //湿度传感器
  给传感器注册一个事件监听器,监听传感器数据的变化:

		this.tempListener = new TempListener();
		this.humidityListener = new HumidityListener();
		
		mSensorManager.registerListener(tempListener, temperature, SensorManager.SENSOR_DELAY_NORMAL);
		mSensorManager.registerListener(humidityListener, humidity, SensorManager.SENSOR_DELAY_NORMAL);
  监听器也很简单,只需实现两个方法即可:

        private class TempListener implements SensorEventListener {
		@Override
		  public final void onSensorChanged(SensorEvent event) {
		    float temperatureValue = event.values[0];
		    BigDecimal bd = new BigDecimal(temperatureValue);
		    double temperature = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//		    Log.i("Sensor", "sensor changed==>" + millibars_of_pressure);
		    TextView tem = (TextView)MainActivity.this.findViewById(R.id.temperature);
		    tem.setText(temperature + "℃");
		  }
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			Log.i("Sensor", "onAccuracyChanged");
		}
	}
  onSensorChanged方法是传感器数据变化时触发,onAccuracyChanged方法是传感器精度发生变化时触发,这个这里暂时不做什么处理,随它去变吧...

  结束时,不要忘了注销监听器:

	@Override
	protected void onPause() {
		super.onPause();

	    mSensorManager.unregisterListener(this.tempListener);
	    mSensorManager.unregisterListener(this.humidityListener);
	    
//	    Log.i("Sensor", "sensor pause");
	}

最后附上整个activity代码:

package org.lhc.thermometer;

import java.math.BigDecimal;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	private SensorManager mSensorManager;
	private Sensor temperature;
	private Sensor humidity;
	
	private SensorEventListener tempListener;
	private SensorEventListener humidityListener;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//		List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
//		for (Sensor sensor : deviceSensors) {
//			Log.i("sensor", "------------------");
//			Log.i("sensor", sensor.getName());
//			Log.i("sensor", sensor.getVendor());
//			Log.i("sensor", Integer.toString(sensor.getType()));
//			Log.i("sensor", "------------------");
//		}
		temperature = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
		humidity = mSensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);
				
		this.tempListener = new TempListener();
		this.humidityListener = new HumidityListener();
		
		mSensorManager.registerListener(tempListener, temperature, SensorManager.SENSOR_DELAY_NORMAL);
		mSensorManager.registerListener(humidityListener, humidity, SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.onPause();
//			Log.i("Sensor", "exit");
			this.finish();
			System.exit(0);
		}
		
		return super.onKeyDown(keyCode, event);
	}

	
	
	@Override
	protected void onPause() {
		super.onPause();

	    mSensorManager.unregisterListener(this.tempListener);
	    mSensorManager.unregisterListener(this.humidityListener);
	    
//	    Log.i("Sensor", "sensor pause");
	}
	
	private class TempListener implements SensorEventListener {
		@Override
		  public final void onSensorChanged(SensorEvent event) {
		    float temperatureValue = event.values[0];    // 利用这些数据执行一些工作
		    BigDecimal bd = new BigDecimal(temperatureValue);
			double temperature = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//		    Log.i("Sensor", "sensor changed==>" + millibars_of_pressure);
		    TextView tem = (TextView)MainActivity.this.findViewById(R.id.temperature);
		    tem.setText(temperature + "℃");
		  }
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
//			Log.i("Sensor", "onAccuracyChanged");
		}
	}
	
	private class HumidityListener implements SensorEventListener {
		@Override
		public final void onSensorChanged(SensorEvent event) {
			float humidityValue = event.values[0];    // 利用这些数据执行一些工作
			BigDecimal bd = new BigDecimal(humidityValue);
			double humidity = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//			Log.i("Sensor", "sensor changed==>" + millibars_of_pressure);
			TextView tem = (TextView)MainActivity.this.findViewById(R.id.humidity);
			tem.setText(humidity + "%");
		}
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
//			Log.i("Sensor", "onAccuracyChanged");
		}
	}
}

转载请注明出处

http://blog.csdn.net/redstarofsleep

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。