当我研究一些关于传感器的东西时,我发现当我使用sensorlistener
(在我的代码中它的mysensorlistener
)来注册我设备中的所有传感器时,它可以获得我需要的东西。如果我将监听器更改为sensoreventlistener
(在我的代码中mysensoreventlistener
),它只能获取加速度计数据。
我想知道我是否可以获得所有数据,只需注册一个sensorevevtlistener
.
这是代码:
package com.example.testaccelerometer;
import com.example.testaccelerometer.R;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AccelerometerReader extends Activity {
TextView atvx;
TextView atvy;
TextView atvz;
TextView otvx;
TextView otvy;
TextView otvz;
SensorManager mySensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.disp);
atvx = (TextView)findViewById(R.id.atvx);
atvy = (TextView)findViewById(R.id.atvy);
atvz = (TextView)findViewById(R.id.atvz);
otvx = (TextView)findViewById(R.id.otvx);
otvy = (TextView)findViewById(R.id.otvy);
otvz = (TextView)findViewById(R.id.otvz);
mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mySensorManager.unregisterListener(mySensorEventListener);
//mySensorManager.unregisterListener(aSensorEventListener);
//mySensorManager.unregisterListener(oSensorEventListener);
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
//mySensorManger.registerListener(mySensorListener, SensorManager.SENSOR_ALL,SensorManager.SENSOR_DELAY_FASTEST);
mySensorManager.registerListener(mySensorEventListener, mySensorManager.getDefaultSensor(Sensor.TYPE_ALL), SensorManager.SENSOR_DELAY_GAME);
//mySensorManager.registerListener(aSensorEventListener, mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
//mySensorManager.registerListener(oSensorEventListener, mySensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
/*private SensorListener mySensorListener = new SensorListener(){
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub
switch(sensor){
case SensorManager.SENSOR_ACCELEROMETER:
atvx.setText("X轴方向的加速度:" + values[0]);
atvy.setText("Y轴方向的加速度:" + values[1]);
atvz.setText("Z轴方向的加速度:" + values[2]);
break;
case SensorManager.SENSOR_ORIENTATION:
otvx.setText("X轴方向的角速度:" + values[0]);
otvy.setText("Y轴方向的角速度:" + values[1]);
otvz.setText("Z轴方向的角速度:" + values[2]);
}
}
};*/
private SensorEventListener mySensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float[] values1;
float[] values2;
switch(event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
values1 = event.values;
atvx.setText("X轴方向的加速度:" + values1[0]);
atvy.setText("Y轴方向的加速度:" + values1[1]);
atvz.setText("Z轴方向的加速度:" + values1[2]);
System.out.println("run....accele");
break;
case Sensor.TYPE_ORIENTATION:
values2 = event.values;
otvx.setText("X轴方向的角速度:" + values2[0]);
otvy.setText("Y轴方向的角速度:" + values2[1]);
otvz.setText("Z轴方向的角速度:" + values2[2]);
System.out.println("run....orien");
break;
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
};
/*
* test
*/
/*private SensorEventListener aSensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float[] values1;
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
values1 = event.values;
atvx.setText("X轴方向的加速度:" + values1[0]); //accelerometer
atvy.setText("Y轴方向的加速度:" + values1[1]);
atvz.setText("Z轴方向的加速度:" + values1[2]);
System.out.println("run....accele");
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
};*/
/*private SensorEventListener oSensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float[] values1;
if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){
values1 = event.values;
otvx.setText("X轴方向的角速度:" + values1[0]); //orientation
otvy.setText("Y轴方向的角速度:" + values1[1]);
otvz.setText("Z轴方向的角速度:" + values1[2]);
System.out.println("run....orien");
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
};*/
}
当我上网时,有人说 SensorEventListener 不能使用 TYPE_ALL 注册所有传感器,但可以使用它来获取传感器列表。喜欢这里的代码:
// 注册加速度传感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
// 注册磁场传感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
// 注册光线传感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_FASTEST);
// 注册方向传感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);