目前,我正在开发一个应用程序来收集加速度计和陀螺仪传感器数据并将其保存到文件中。我正在使用警报管理器来安排警报每15分钟触发一次,一旦警报触发,它将开始在后台收集数据。
但我面临的问题是,有时加速计传感器会为所有三轴返回零。任何人都可以帮我解决这个问题吗?
以下是我的IntentService类代码的一部分:
public class SaveSensorDataService extends IntentService{
private SensorManager sensorManager;
private Sensor accelerometerSensor;
private Sensor gyroscopeSensor;
private float[] gyroscopeValues = new float[3];
private float[] accelerometerValues = new float[3];
private SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
accelerometerValues = sensorEvent.values;
}
if(sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE){
gyroscopeValues = sensorEvent.values;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
@Override
public void onCreate() {
super.onCreate();
//Called by the system when the service is first created.
//SensorManager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//Gyroscope Sensor
gyroscopeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListener, gyroscopeSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
try { //Wait for the sensor data from the callback methods.
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (writeFile()) {
Log.i(TAG, "Succefully saved into file.");
sendNotification();
Log.i(TAG, "Wakelock released.");
completeWakefulIntent(intent);
}
}
@Override
public void onDestroy() {
super.onDestroy();
//unregister the listener.
sensorManager.unregisterListener(sensorEventListener);
}
}