I use the location.getTime() to get the actual time in the onLocationChanged method. For a test, I have changed the system date from 09.11 to 09.08. When I start the application, in the first 60-70 results the location.getTime() returns the system date. After that, it returns the correct date. Is there any strategy to validate the location 's time?
For logging I use the following code:
@Override
public void onLocationChanged(Location location) {
Calendar gpsCalendar = Calendar.getInstance();
gpsCalendar.setTimeInMillis(location.getTime());
int gpsYear = gpsCalendar.get(Calendar.YEAR);
int gpsMonth = gpsCalendar.get(Calendar.MONTH);
int gpsDay = gpsCalendar.get(Calendar.DAY_OF_MONTH);
Calendar sysCalendar = Calendar.getInstance();
sysCalendar.setTimeInMillis(System.currentTimeMillis());
int sysYear = sysCalendar.get(Calendar.YEAR);
int sysMonth = sysCalendar.get(Calendar.MONTH);
int sysDay = sysCalendar.get(Calendar.DAY_OF_MONTH);
mLocationCounter++;
Log.i("GPSTimeCheck",
"Counter: " + mLocationCounter +
" | Provider: " + location.getProvider() +
" | Accuracy: " + location.getAccuracy() +
" | GPS: " + gpsYear + "/" + gpsMonth + "/" + gpsDay +
" | System: " + sysYear + "/" + sysMonth + "/" + sysDay
);
//Other cool stuff here
}
The results in logcat with GPSTimeCheck tag:
Counter: 1 | Provider: fused | Accuracy: 20.0 | GPS: 2017/8/8 | System: 2017/8/8
Counter: 2 | Provider: fused | Accuracy: 20.0 | GPS: 2017/8/8 | System: 2017/8/8
Counter: 3 | Provider: fused | Accuracy: 20.0 | GPS: 2017/8/8 | System: 2017/8/8
Counter: 4 | Provider: fused | Accuracy: 14.142 | GPS: 2017/8/8 | System: 2017/8/8
Counter: 5 | Provider: fused | Accuracy: 11.547 | GPS: 2017/8/8 | System: 2017/8/8
Counter: 6 | Provider: fused | Accuracy: 10.0 | GPS: 2017/8/8 | System: 2017/8/8
After 70 location changes with Accuracy: 10.0 and with GPS: 2017/8/8 and System: 2017/8/8 dates it returns the correct date with location.getTime()
Counter: 73 | Provider: fused | Accuracy: 10.0 | GPS: 2017/8/11 | System: 2017/8/8