下面以摇一摇的实现来演示传感器开发的步骤:
1、声明一个SensorManager对象,该对象从系统服务Context.SENSOR_SERVICE中获取实例;mSensroMgr.registerListener(this, mSensroMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);4、重写onPause方法,在该方法中注销传感器事件,代码示例如下:
mSensroMgr.unregisterListener(this);
下面是摇一摇功能的核心代码:
float[] values = event.values; if (sensorType == Sensor.TYPE_ACCELEROMETER) { if ((Math.abs(values[0]) > 15 || Math.abs(values[1]) > 15 || Math.abs(values[2]) > 15)) { tv_shake.setText(getNowDateTime()+" 恭喜您摇一摇啦"); //系统检测到摇一摇事件后,震动手机提示用户 mVibrator.vibrate(500); } }
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import android.app.Activity; 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.os.Vibrator; import android.util.Log; import android.widget.TextView; import com.example.exmcamera.R; public class SensorActivity extends Activity implements SensorEventListener { private static final String TAG = "SensorActivity"; private TextView tv_sensor; private TextView tv_shake; private TextView tv_light; private TextView tv_distance; private SensorManager mSensroMgr; private Vibrator mVibrator; private String[] mSensorType = { "加速度", "磁场", "方向", "陀螺仪", "光线", "压力", "温度", "距离", "重力", "线性加速度", "旋转矢量", "湿度", "环境温度", "无标定磁场", "无标定旋转矢量", "未校准陀螺仪", "特殊动作", "步行检测", "计步器", "地磁旋转矢量"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sensor); tv_sensor = (TextView) findViewById(R.id.tv_sensor); tv_shake = (TextView) findViewById(R.id.tv_shake); tv_light = (TextView) findViewById(R.id.tv_light); tv_distance = (TextView) findViewById(R.id.tv_distance); mSensroMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); showSensorInfo(); } private void showSensorInfo() { List<Sensor> sensorList = mSensroMgr.getSensorList(Sensor.TYPE_ALL); ArrayList<String> sensorNameList = new ArrayList<String>(); String show_content = "当前支持的传感器包括:\n"; for (Sensor sensor : sensorList) { sensorNameList.add(sensor.getName()); String content = String.format("%s:%s\n", mSensorType[sensor.getType()-1], sensor.getName()); show_content += content; } tv_sensor.setText(show_content); } private String getNowDateTime() { SimpleDateFormat s_format = new SimpleDateFormat("HH:mm:ss"); Date d_date = new Date(); String s_date = ""; s_date = s_format.format(d_date); return s_date; } @Override protected void onPause() { super.onPause(); mSensroMgr.unregisterListener(this); } @Override protected void onResume() { super.onResume(); mSensroMgr.registerListener(this, mSensroMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); mSensroMgr.registerListener(this, mSensroMgr.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL); mSensroMgr.registerListener(this, mSensroMgr.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); if (sensorType == Sensor.TYPE_ACCELEROMETER) { // values[0]:X轴,values[1]:Y轴,values[2]:Z轴 float[] values = event.values; if (sensorType == Sensor.TYPE_ACCELEROMETER) { if ((Math.abs(values[0]) > 15 || Math.abs(values[1]) > 15 || Math.abs(values[2]) > 15)) { tv_shake.setText(getNowDateTime()+" 恭喜您摇一摇啦"); //系统检测到摇一摇事件后,震动手机提示用户 mVibrator.vibrate(500); } } } else if (sensorType == Sensor.TYPE_LIGHT) { float light_strength = event.values[0]; tv_light.setText(getNowDateTime()+" 当前光线强度为"+light_strength); } else if (sensorType == Sensor.TYPE_PROXIMITY) { float distance = event.values[0]; tv_distance.setText(getNowDateTime()+" 有不明物体接近!距离"+distance+"厘米"); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { //当传感器精度改变时回调该方法,一般无需处理 } }