即时显示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;