即时显示gsensor的数据,可以在调试重力感应器驱动和测试手机性能时起到很好的作用。类似的,SensorEventListener还可以用在其他感应器的场合,比如光感应、地磁感应。
import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class Activity01 extends Activity implements SensorEventListener{ //在继承activity类的同时使用listsen接口 private final String TAG = "zhangcheng"; private SensorManager mSensorManager; private Sensor sensor; private float mLastX,mLastY,mLastZ; private String sX,sY,sZ; private TextView mTextViewX = null; private TextView mTextViewY = null; private TextView mTextViewZ = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity01); mTextViewX = (TextView)findViewById(R.id.TextView1); mTextViewY = (TextView)findViewById(R.id.TextView2); mTextViewZ = (TextView)findViewById(R.id.TextView3); //绑定显示控件句柄 mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); if(mSensorManager == null){ Log.i(TAG,"sensor not supported"); } mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); } public void onAccuracyChanged(Sensor arg0, int arg1){ } public void onSensorChanged(SensorEvent event){ //在activity中完成该接口函数 if(event.sensor == null){ return; } if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ mLastX = event.values[0]; mLastY = event.values[1]; mLastZ = event.values[2]; sX = String.valueOf(mLastX); sY = String.valueOf(mLastY); sZ = String.valueOf(mLastZ); mTextViewX.setText(sX); mTextViewY.setText(sY); mTextViewZ.setText(sZ); //由于该接口函数不断刷新,所以可以把控件显示函数放在这里显示 } } protected void onStop(){ //同样在退出activity时要注销监听 super.onStop(); if(mSensorManager != null){ mSensorManager.unregisterListener(this); mSensorManager = null; } } }
该函数可在一个activity中即时显示gsensor数据,方便查看。
========================================================================================================================
现实中的很多手机应用会到gsensor这个感应器,比如手机的倾斜角度,手机的运动方向和加速度。如果我们要在一个服务中通过监控gsensor的数据来判断手机的静止或者运动,进而作相应处理。流程是:原始数据 -> 低通滤波 -> 重力在3个方向的分量 -> 判断即可。以下步骤:
(1)服务定义时要implements SensorEventListener接口,以下是一些变量。
private SensorManager mSensorManager; //sensor管理器 private Sensor sensor; private float[] curGravity = new float[3]; //xyz重力数据 private lowPassFilter filter1; private lowPassFilter filter2; private firLowPassFilter firFilter1; private firLowPassFilter firFilter2; private firLowPassFilter firFilter3; //低通滤波的一些类的内容 private int mAccSkipCnt = 0; //抖动计数 private static final int __ACC_SKIP_SAMPLES = 30; // 跳过刚开始的若干个加速度采样 private int _NTAPS = 6; private double[] h = { 0.125514644795420960, 0.414388923238107440, -0.013420976983735622, -0.013420976983735622, 0.414388923238107440, 0.125514644795420960 }; private boolean DeviceFlating = false; //手机是否平躺了
(2)在服务onCreate函数中定义sensor相关变量,并设置低通滤波的一些系数
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); filter1 = new lowPassFilter(1, 20); filter2 = new lowPassFilter(0, 0.9f); firFilter1 = new firLowPassFilter(_NTAPS, h); firFilter2 = new firLowPassFilter(_NTAPS, h); firFilter3 = new firLowPassFilter(_NTAPS, h);
(3)SensorEventListener接口中要完成的成员函数
public void onSensorChanged(SensorEvent event){ if(event.sensor == null){ return; } mIsSmartStayOn= Settings.System.getInt(cr,"isSmartStayEnabled",0) == 1 ? true : false;//人眼识别打开时才启动重力监听 if(mIsSmartStayOn == true){ Log.i(TAG,"gsensor respond"); curGravity[0] = (float)firFilter1.filter1((double)event.values[0]); curGravity[1] = (float)firFilter2.filter1((double)event.values[1]); curGravity[2] = (float)firFilter3.filter1((double)event.values[2]); curGravity = filter1.SMAFilter(curGravity); //取得三轴重力数据并滤波处理 if(mAccSkipCnt < __ACC_SKIP_SAMPLES) { mAccSkipCnt++; return ; //待gsensor读数稳定才继续后面的处理 } float[] linearAcc = new float[]{ event.values[0]-curGravity[0], event.values[1]-curGravity[1], event.values[2]-curGravity[2] }; double linearLen = KonkaGeneHelper.vecLength(linearAcc); //三轴数据平方和相加开根号的结果 Log.w(TAG,"linearLen is "+linearLen); if(linearLen < 0.6f && DeviceFlating == false){ //如果手机一开始没有静止并且linearLen小于0.6 DeviceFlating = true; //手机趋近于静止状态(不论方向) } if(linearLen > 1.0f && DeviceFlating == true && pm.isScreenOn() == true){ DisplayEyeIcon(1); //如果手机静止的,并且gsensor检测到linearLen大于1且手机亮屏 freshDetect(); DeviceFlating = false; //马上启动人眼识别 } } }
通过以上过程,可以参考其中的重力计算和判断流程用在其他场合。
==================================================================
手机摇一摇功能,仅需要在上面sensor读值获取的地方,加上判断即可。如下的示例,摇一摇时震动并toast提示。
private static final int SENSOR_SHAKE = 10; //自定义消息
..................................
int medumValue = 19;// if (Math.abs(mLastX) > medumValue || Math.abs(mLastX) > medumValue || Math.abs(mLastX) > medumValue) { vibrator.vibrate(200); Message msg = new Message(); msg.what = SENSOR_SHAKE; handler.sendMessage(msg); }
..............................
Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SENSOR_SHAKE: Toast.makeText(MainActivity.this, "检测到摇晃,执行操作!", Toast.LENGTH_SHORT).show(); Log.i(TAG, "检测到摇晃,执行操作!"); break; } } };
参见http://blog.csdn.net/jason0539/article/details/10154997
==================================================================
测试气压计的应用如下:
public class MainActivity extends Activity { private SensorManager mSensorManager=null; private Sensor mSensor=null; private TextView textView1=null; private Button button1=null; private Button button2=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1=(TextView)findViewById(R.id.textView1); /*获取系统服务(SENSOR_SERVICE)返回一个SensorManager对象*/ mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE); /*通过SensorManager获取相应的(压力传感器)Sensor类型对象*/ mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE); /*注册相应的SensorService*/ button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { mSensorManager.registerListener(mSensorEventListener, mSensor , SensorManager.SENSOR_DELAY_NORMAL); } }); /* 销毁相应的SensorService * 很关键的部分,注意,说明文档中提到,即使Activity不可见的时候,感应器依然会继续工作 * 所以一定要关闭触发器,否则将消耗用户大量电量*/ button2=(Button)findViewById(R.id.button2); button2.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { mSensorManager.unregisterListener(mSensorEventListener, mSensor); } }); } /*声明一个SensorEventListener对象用于侦听Sensor事件,并重载onSensorChanged方法*/ private final SensorEventListener mSensorEventListener=new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType()==Sensor.TYPE_PRESSURE){ /*压力传感器返回当前的压强,单位是百帕斯卡hectopascal(hPa)。*/ float pressure=event.values[0]; textView1.setText(String.valueOf(pressure)+"hPa"); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
参考原文:http://www.cnblogs.com/snowdrop/articles/1802092.html