传感器应用开发

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

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) {

	}

}

 布局效果:


                
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值