我一直致力于一个神秘的应用程序,允许用户同时获取多个传感器值并将其显示在Listview上。然而,令我沮丧的是,我从来没有能够获得加速度计值,这就是它。我已经尝试了多种方式来注册所有可用的传感器,如陀螺仪,磁场,方向等。它们都没有检索值,因为Accelerometer优先考虑并且每次都被触发。有没有办法同时注册多个传感器并从中获取数据以更新UI。
mAccelerometerListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
//Update UI
}
}
};
mGyroscopeListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
//Update Ui
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
};
mMagnetometerListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
//Update UI
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
};
if (Config.isSensorPresent(ACCELERATION)) {
//sendMessageToService(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(mAccelerometerListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}
// Detect whether the device has Gyroscope on it
if (Config.isSensorPresent(GYROSCOPE)) {
//sendMessageToService(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(mGyroscopeListener, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
// Detect whether the device has Magnetic Field Sensor
if (Config.isSensorPresent(MAGNETIC_FIELD)) {
//sendMessageToService(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener(mMagnetometerListener, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
}
这似乎不会同时更新所有这些内容。哎呀,它甚至没有触发其他类型的传感器。
尝试了Is there a way to retrieve multiple sensor data in Android建议的方法。对我不起作用。
答案 0 :(得分:1)
我认为第8行可能是您的问题。但是,您也可以尝试以下代码:
public class MainActivity extends AppCompatActivity implements
SensorEventListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SensorManager sm = (SensorManager)this.getSystemService(SENSOR_SERVICE);
Sensor se = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener((SensorEventListener) this, se, SensorManager.SENSOR_DELAY_GAME);
Sensor se1 = sm.getDefaultSensor(TYPE_GYROSCOPE);
sm.registerListener((SensorEventListener) this, se1, SensorManager.SENSOR_DELAY_GAME);
Sensor se2 = sm.getDefaultSensor(TYPE_MAGNETIC_FIELD);
sm.registerListener((SensorEventListener) this, se2, SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
//Update UI
}
if(event.sensor.getType()==Sensor.TYPE_GYROSCOPE){
//UpdateUI
}
if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){
//UpdateUI
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
该代码应该起作用,因为只要上述三个传感器值中的任何一个发生更改(几乎总是),就会调用此函数(OnSensorChanged())。 如果不是这样,那么问题可能出在您的updateUI代码中。
答案 1 :(得分:0)
有一个传感器事件监听器并将所有传感器注册到它,但是在onSensorChanged()方法中你可以找出哪个传感器已经调用它并采取相应的行动。
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE)//update gyro
if (event.sensor.getType() == //acclometer)update acc
.
.
.
so on
}