我一直在开发一个神秘的应用程序,它允许用户同时获取多个传感器值并将它们显示在 Listview 上。然而,令我沮丧的是,我一直无法通过获取加速度计值,仅此而已。我尝试了多种方法来注册所有可用的传感器,如陀螺仪、磁场、方向等。由于加速度计优先并每次都被触发,因此它们都没有检索值。有没有办法同时注册多个传感器并从中获取数据以更新 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 中建议的方法。对我不起作用。