Edit:
This is not talking about precision issues, from the code and log below, you can see that I requested to sleep for 1 second, but the result was almost 200 seconds, sometimes it could jump to 600 seconds, this cannot be a precision issue..
I was using handlerthread before and sometimes the job posted to handler just does not start on time, to get more details I changed it to the basic Thread, and it turns out the Thread.sleep() is the issue, but I'm not sure how to fix this, what could be the possible reasons?
hGpsThread = new Thread(mGpsWorker);
hGpsThread.start();
private final Runnable mGpsWorker = new Runnable() {
@Override
public void run() {
long lastGpsRequestTime = 0;
l.Write("GPS thread started.");
while (isRunning) {
l.Write("GPS thread loop start.");
try {
long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis >= lastGpsRequestTime + gpsUpdateInterval) {
l.Write("Requesting location update");
gpslib.getLocation();
lastGpsRequestTime = currentTimeMillis;
}
l.Write("GPS thread before sleep");
Thread.sleep(1000);
l.Write("GPS thread after sleep");
} catch (InterruptedException e) {
}
l.Write("GPS thread loop end.");
}
l.Write("GPS thread ended.");
}
};
NOTE: getLocation() calls requestLocationUpdates using a looper from another thread, so location updating should not affect this thread, as I understand.
getLocation() also creates a Timer and schedules a timeout, could that be the issue? as I understand it should not be an issue.
This is the log: (this only happens occasionally, say the chance is close to 0.5%)
Wed Jul 10 11:45:46 AEST 2013 GPS thread loop start.
Wed Jul 10 11:45:46 AEST 2013 GPS thread before sleep
Wed Jul 10 11:49:04 AEST 2013 GPS thread after sleep
Wed Jul 10 11:49:04 AEST 2013 GPS thread loop end.
Thanks
The testing environment is: HTC Aria, Android 2.2 And looks like it only happens when running on battery, but my app doesn't act differently on charging status.