传感器应用开发

kerrysimon 2014-08-22

传感器开发基本代码流程:

onCreate():

SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

rate = SensorManager.SENSOR_DELAY_NORMAL;

onResume();

sm.registerListener(SensorEventListener listener,Sensor sensor, int rate);

onStop():

sm.unregisterListener(SensorEventListener listener);

传感器开发实例 代码片段:

package com.bob.mycrazy;


import android.app.Activity;
import android.app.Fragment;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class SensorFragment extends Fragment implements SensorEventListener {
	private EditText etorientation, etmagnetic, ettemperatura, etlight,
			etpresure;
	private static SensorManager sm;//sensor管理器

	public static SensorFragment newInstance(SensorManager sensorManager) {
		SensorFragment fragment = new SensorFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		sm = sensorManager;
		return fragment;
	}

	public SensorFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.fragment_sensor, container, false);
		etorientation = (EditText) view.findViewById(R.id.etorientation);
		etmagnetic = (EditText) view.findViewById(R.id.etmagnetic);
		ettemperatura = (EditText) view.findViewById(R.id.ettemperatura);
		etlight = (EditText) view.findViewById(R.id.etlight);
		etpresure = (EditText) view.findViewById(R.id.etpresure);

		return view;
	}

	@Override
	public void onResume() {
		//为方向传感器注册监听器
		sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				SensorManager.SENSOR_DELAY_NORMAL);
		//为磁场传感器注册监听器
		sm.registerListener(this,
				sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
				SensorManager.SENSOR_DELAY_NORMAL);
		//为温度传感器注册监听器
		sm.registerListener(this,
				sm.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
				SensorManager.SENSOR_DELAY_NORMAL);
		//为光传感器注册监听器
		sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_LIGHT),
				SensorManager.SENSOR_DELAY_NORMAL);
		//为压力传感器注册监听器
		sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PRESSURE),
				SensorManager.SENSOR_DELAY_NORMAL);
		super.onResume();
	}

	@Override
	public void onStop() {
		//退出时取消注册监听器
		sm.unregisterListener(this);
		super.onStop();
	}

	@Override
	public void onPause() {
		//暂停时取消注册监听器
		sm.unregisterListener(this);
		super.onPause();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	//SensorEventListener接口必须实现的方法
	@Override
	public void onSensorChanged(SensorEvent event) {
		//获取传感器返回的值
		float[] values = event.values;
		//获取触发监听的传感器类型
		int type = event.sensor.getType();
		StringBuilder sb = new StringBuilder();
		switch (type) {
		//方向传感器
		case Sensor.TYPE_ORIENTATION:
			sb.append("绕Z轴转过的角度:" + values[0]);
			sb.append("\n绕x轴转过的角度:" + values[1]);
			sb.append("\n绕y轴转过的角度:" + values[2]);
			etorientation.setText(sb.toString());
			break;
		//磁场传感器
		case Sensor.TYPE_MAGNETIC_FIELD:
			sb.append("x方向上的角度:" + values[0]);
			sb.append("\ny方向上的角度:" + values[1]);
			sb.append("\nz方向上的角度:" + values[2]);
			etmagnetic.setText(sb.toString());
			break;
		//温度传感器	
		case Sensor.TYPE_AMBIENT_TEMPERATURE:
			sb.append("当前温度为:" + values[0]);
			ettemperatura.setText(sb.toString());
			break;
		//光传感器	
		case Sensor.TYPE_LIGHT:
			sb.append("当前光的强度为:" + values[0]);
			etlight.setText(sb.toString());
			break;
		//压力传感器	
		case Sensor.TYPE_PRESSURE:
			sb.append("当前压力为:" + values[0]);
			etpresure.setText(sb.toString());
			break;
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

}

 布局效果:


                传感器应用开发
 

相关推荐

行森啊有点追求啊 / 0评论 2019-10-20