我有一个Android应用程序,它以100hz的频率从加速度传感器和旋转矢量传感器获取数据。它还每1秒从GPS获取一次位置。该应用程序每10毫秒对计时器上收集的数据进行一些计算。并将结果存储在一个csv文件中,每3分钟将其上传到Firebase。
问题是,即使位置侦听器在单独的线程上运行并且传感器侦听器在单独的线程上运行,结果csv文件上仍有许多连续的重复值,并且写入文件和上载到Firebase都是异步完成的
该应用程序的iOS版本是使用swift本地编写的,过程大致相同,但没有此问题。
mSensorThread = new HandlerThread("Sensor thread", Thread.MAX_PRIORITY);
mSensorThread.start();
Handler mSensorHandler = new Handler(mSensorThread.getLooper());
mSensorManager.registerListener(this, mAccelerometer, 10000,9000,mSensorHandler);
mSensorManager.registerListener(this, mRotationVector, 10000,9000, mSensorHandler);
mLocationThread = new HandlerThread("Location thread", Thread.MAX_PRIORITY);
mLocationThread.start();
Looper looper = mLocationThread.getLooper();
LocationServices.FusedLocationApi.requestLocationUpdates (mGoogleApiClient, mLocationRequest, this, looper).addStatusListener(new PendingResult.StatusListener() {
@Override
public void onComplete(Status status) {
startRecording();
}
});
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// some calculations on sensors data.
}
}, 0, period);
private void writeToCsvFileAndFirebase(final String data, final Date lastDate) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// write the file and upload to firebase every 3 minuets.
// this function is called from the scheduled timer.
}
});
thread.start();
}