现在的位置: 首页 > 综合 > 正文

gsensor

2013年03月13日 ⁄ 综合 ⁄ 共 4178字 ⁄ 字号 评论关闭

即时显示gsensor的数据,可以在调试重力感应器驱动和测试手机性能时起到很好的作用。类似的,SensorEventListener还可以用在其他感应器的场合,比如光感应、地磁感应。这里用两种方式来完成读取并显示gsensor数据的功能,一种是activity和SensorEventListener监听分离,在不同类中完成;另一种是在同一个activity中完成。

(1)activity和SensorEventListener监听分离

        Acvitivity启动时初始化SensorEventListener,在退出activity时注销Listener。

[java] view
plain
copy

  1. import android.os.Bundle;  
  2. import android.app.Activity;  
  3.   
  4. import android.util.Log;  
  5. import android.view.Menu;  
  6. import android.widget.TextView;  
  7.   
  8. public class Activity01 extends Activity {  
  9.     private final String TAG = "zhangcheng";  
  10.     SensorListenerTest sensorListenerTest;        //sensor监听类,同一个包里的类被调用时不需要import  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_activity01);  
  15.         sensorListenerTest = new SensorListenerTest(this);     //监听类的构造函数,activity通过this句柄跟监听类绑定  
  16.     }  
  17.   
  18.     protected void onResume(){  
  19.         super.onResume();  
  20.         sensorListenerTest.enableSensor();          //activity启动后使能sensor监听  
  21.     }  
  22.       
  23.     protected void onStop(){  
  24.         super.onStop();  
  25.         sensorListenerTest.disableSensor();                    //出于系统控制原因,在退出activity时注销监听  
  26.     }  
  27. }  

      SensorListenerTest类,监听gsensor数据。注意import android.hardware.sensor的四个类。

[java] view
plain
copy

  1. import android.content.Context;  
  2. import android.hardware.Sensor;  
  3. import android.hardware.SensorEvent;  
  4. import android.hardware.SensorEventListener;  
  5. import android.hardware.SensorManager;  
  6. import android.util.Log;  
  7.   
  8. public class SensorListenerTest implements SensorEventListener   {   //使用listsen的接口,所以必须实现接口的两个成员函数  
  9.     private final String TAG = "zhangcheng";  
  10.     private SensorManager mSensorManager;  
  11.     private Sensor sensor;  
  12.     private float mLastX,mLastY,mLastZ;    // X Y Z三轴数据  
  13.     private Context mContext;  
  14.       
  15.     public SensorListenerTest(Context context){  
  16.         mContext = context;  
  17.     }  
  18.       
  19.     public void enableSensor(){  
  20.         mSensorManager = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);   //获得系统的sensor服务  
  21.         sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);     //具体要操作的sensor类型  
  22.           
  23.         if(mSensorManager == null){  
  24.             Log.i(TAG,"sensor not supported");  
  25.         }  
  26.         mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);  //以普通采样率注册监听器  
  27.     }  
  28.       
  29.     public void disableSensor(){  
  30.         if(mSensorManager != null){  
  31.             mSensorManager.unregisterListener(this);       //注销监听器  
  32.             mSensorManager = null;  
  33.         }  
  34.     }  
  35.       
  36.     public void onAccuracyChanged(Sensor arg0, int arg1){         
  37.     }  
  38.       
  39.     public void onSensorChanged(SensorEvent event){     //必须完成SensorEventListener的响应函数  
  40.         if(event.sensor == null){  
  41.             return;  
  42.         }  
  43.           
  44.         if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){  
  45.             mLastX = event.values[0];  
  46.             mLastY = event.values[1];  
  47.             mLastZ = event.values[2];        //得到XYZ三轴数据  
  48.               
  49.             String sX = String.valueOf(mLastX);  
  50.             String sY = String.valueOf(mLastY);  
  51.             String sZ = String.valueOf(mLastZ);    //利用String的成员函数valueOf快速将float型转成String型  
  52.               
  53.             Log.i(TAG,"mLastX == "+sX);  
  54.             Log.i(TAG,"mLastY == "+sY);  
  55.             Log.i(TAG,"mLastZ == "+sZ);       //在Logcat中即时提示读出的gsensor数据  
  56.         }  
  57.     }  
  58. }  

       运行结果诸如在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来显示。

[java] view
plain
copy

  1. import android.hardware.Sensor;  
  2. import android.hardware.SensorEvent;  
  3. import android.hardware.SensorEventListener;  
  4. import android.hardware.SensorManager;  
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.util.Log;  
  9. import android.view.Menu;  
  10. import android.widget.TextView;  

抱歉!评论已关闭.