即时显示gsensor的数据,可以在调试重力感应器驱动和测试手机性能时起到很好的作用。类似的,SensorEventListener还可以用在其他感应器的场合,比如光感应、地磁感应。这里用两种方式来完成读取并显示gsensor数据的功能,一种是activity和SensorEventListener监听分离,在不同类中完成;另一种是在同一个activity中完成(*)。
(1)activity和SensorEventListener监听分离
Acvitivity启动时初始化SensorEventListener,在退出activity时注销Listener。
- import android.os.Bundle;
- import android.app.Activity;
- import android.util.Log;
- import android.view.Menu;
- import android.widget.TextView;
- public class Activity01 extends Activity {
- private final String TAG = "zhangcheng";
- SensorListenerTest sensorListenerTest; //sensor监听类,同一个包里的类被调用时不需要import
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_activity01);
- sensorListenerTest = new SensorListenerTest(this); //监听类的构造函数,activity通过this句柄跟监听类绑定
- }
- protected void onResume(){
- super.onResume();
- sensorListenerTest.enableSensor(); //activity启动后使能sensor监听
- }
- protected void onStop(){
- super.onStop();
- sensorListenerTest.disableSensor(); //出于系统控制原因,在退出activity时注销监听
- }
- }
SensorListenerTest类,监听gsensor数据。注意import android.hardware.sensor的四个类。
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.util.Log;
- public class SensorListenerTest implements SensorEventListener { //使用listsen的接口,所以必须实现接口的两个成员函数
- private final String TAG = "zhangcheng";
- private SensorManager mSensorManager;
- private Sensor sensor;
- private float mLastX,mLastY,mLastZ; // X Y Z三轴数据
- private Context mContext;
- public SensorListenerTest(Context context){
- mContext = context;
- }
- public void enableSensor(){
- mSensorManager = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE); //获得系统的sensor服务
- sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //具体要操作的sensor类型
- if(mSensorManager == null){
- Log.i(TAG,"sensor not supported");
- }
- mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); //以普通采样率注册监听器
- }
- public void disableSensor(){
- if(mSensorManager != null){
- mSensorManager.unregisterListener(this); //注销监听器
- mSensorManager = null;
- }
- }
- public void onAccuracyChanged(Sensor arg0, int arg1){
- }
- public void onSensorChanged(SensorEvent event){ //必须完成SensorEventListener的响应函数
- if(event.sensor == null){
- return;
- }
- if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
- mLastX = event.values[0];
- mLastY = event.values[1];
- mLastZ = event.values[2]; //得到XYZ三轴数据
- String sX = String.valueOf(mLastX);
- String sY = String.valueOf(mLastY);
- String sZ = String.valueOf(mLastZ); //利用String的成员函数valueOf快速将float型转成String型
- Log.i(TAG,"mLastX == "+sX);
- Log.i(TAG,"mLastY == "+sY);
- Log.i(TAG,"mLastZ == "+sZ); //在Logcat中即时提示读出的gsensor数据
- }
- }
- }
运行结果诸如在Logcat中显示:
01-02 23:28:41.634: I/zhangcheng(2238): mLastX == -0.181
01-02 23:28:41.634: I/zhangcheng(2238): mLastY == 0.143
01-02 23:28:41.634: I/zhangcheng(2238): mLastZ == 9.979
01-02 23:28:41.808: I/zhangcheng(2238): mLastX == -0.143
01-02 23:28:41.808: I/zhangcheng(2238): mLastY == 0.172
01-02 23:28:41.808: I/zhangcheng(2238): mLastZ == 10.008
(2)单一activity完成监听。上述方法完成后在Logcat中显示读数不太直观,可以直接在activity上用textview来显示。
- 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; //马上启动人眼识别
- }
- }
- }
通过以上过程,可以参考其中的重力计算和判断流程用在其他场合。