6

My app uses LocationListener to keep track of the current location. So long as the GPS Provider is providing regular fixes this works well. However, I want my app to alert the user if the location is no longer reliable because the fix is no longer current. I have therefore used a timeCheckHandler to call getLastKnownLocation every few seconds.

My problem is that even when accurate fixes are being received frequently the time returned by applying getTime() to the location returned by getLastKnownLocation is generally older than the current time returned by System.currentTimeMillis(), often by about 20 seconds.

I have investigated the problem further by adding code to onLocationChanged(arg0) to log the time of the fix (arg0.getTime()) and the current time (System.currentTimeMillis()). Again I see a difference of about 20 seconds.

The code currently reads as follows:

    public void onLocationChanged(Location arg0) {
    mapview.handleLocationChanged(mapview, arg0.getLatitude(), arg0.getLongitude(), arg0.getBearing(), arg0.getAccuracy(), "GPS fix");
    addDebugNote("Fix received at time: "+Long.toString(arg0.getTime()/1000)+" Now: "+Long.toString((System.currentTimeMillis())/1000));
}

and typical output to my Debug file reads:

Fix received at time: 1292091908 Now: 1292091928

Why should I be seeing this difference between the fix time and the current system time?

Do I have to accept that a difference of around 20 seconds is normal?

prepbgg
  • 3,564
  • 10
  • 39
  • 51
  • I had the same problem and I added: `arg0.setTime( System.currentTimeMillis() );` as first line in `onLocationChanged(Location arg0)` method to "correct" the timestamp. I'm not sure if this will create any side effects later – ApollonDigital Apr 08 '15 at 16:19

2 Answers2

20

GPS location time comes independently of your network provider time/device time. System.currentTimeMillis() will give you device time set on your device.

If you want to know how recent the point is you can:

  1. Synchronize both the times ( GPS and device ) in your code at application start by taking the difference between both as soon as you get first GPS location update. At that instant query device time and see what's the difference in both. Save this difference in variable.

  2. Use this as a correction factor in subsequent location updates to know the exact time based on the reference frame you need. ( Device time or GPS)

Also I had found that using NETWORK as location provider you may get device time only. So if you are listening on updates from both ( GPS and network ) , you may also need to distinguish this using location_obj.getProvider() and filter out GPS provider.

Pritam
  • 2,367
  • 5
  • 34
  • 50
  • 1
    Thanks. This is very helpful. (I imagine GPS time must be very accurate in order for it to work with such precision. I wonder why phone networks simply don't synchronise with it.) – prepbgg Dec 19 '10 at 17:54
  • good point, maybe its just more likely that network is on than gps? either way, if your phone sync the time to gps wouldn't we encounter a network time that was off when we tried to getTime()? so basically we would have to sunc timers when we used network for location updates – Goku Dec 03 '12 at 15:37
1

Repeating the test today I have found that the difference between the GPS time and the System time is 22 seconds. This issue is discussed elsewhere on the web and it seems that it is normal for there to be a difference between GPS time and the phone's system time (which in my case is set to be updated automatically from the network.) One factor is that GPS time is about 15 or 17 seconds (depending on which source is correct) from UTC time ... it is out-of-sync because GPS time has not been updated since 1980 for periodic "leap seconds."

Bearing this in mind I think the answer to my need to check how current the latest fix is will be to compare the current system time with the system time (not the GPS time) of the latest fix.

prepbgg
  • 3,564
  • 10
  • 39
  • 51
  • Re-reading the Android reference for Location I am reminded that getTime() returns the UTC time of the fix, and I presume that I can rely on this being accurate. Therefore it must be the phone's system time that is inaccurate. Reading further on the internet I see that it is usual for phones' clocks to drift out of sync by many seconds. – prepbgg Dec 13 '10 at 08:45
  • Yes: there is a ZDNet article from 2012 stating that android devices (then) did not account for the 15 leap seconds added to utc since 1970, while gps should be accurate. – FrankKrumnow Aug 21 '19 at 13:43