5

I'm seeing in docs that the object Location has a method getTime().

In my app, I need the server time, but sometimes, app can be offline, so I have no choice to get cel time. I would gladly use Location.getTime, but it is not clear to me where this time is coming from?

The Cel or the GPS Satelite???

Is it a reliable data for getting the current hour when app is offline?

Juliatzin
  • 18,455
  • 40
  • 166
  • 325

2 Answers2

3

As discussed in this question, Location.getTime() returns either

  • the device time (System.currentTimeMillis()) if Location.getProvider().equals(LocationManager.NETWORK_PROVIDER)

or

  • the GPS (satellite) time (in milliseconds but with 1s precision) if Location.getProvider().equals(LocationManager.GPS_PROVIDER)

Since the GPS location determination is based on the knowledge of the precise time, I would say it is at least as reliable as the time you would get from a server. You can apply your local time zone to the GPS timestamp to get a human readable time, which sould be equal to a properly synced device time (in my case most of the devices where not, so it was even better to use the GPS time).

Community
  • 1
  • 1
F1iX
  • 823
  • 6
  • 12
0

It is useful when you don't need a very precise time and you just want to know the date only regardless to precise time. Since as the documentation says:

All locations generated by the LocationManager are guaranteed to have a valid UTC time, however remember that the system time may have changed since the location was generated.

Also take a look at getElapsedRealtimeNanos() it seems to be more precise.

AMS
  • 104
  • 9
  • 1
    yes, but the question is : where is this time coming from – Juliatzin Mar 17 '17 at 00:37
  • You should read this article, very simple and helpful: https://en.m.wikipedia.org/wiki/GPS_disciplined_oscillator – AMS Mar 17 '17 at 05:56
  • And don't forget that some devices has A-GPS which may depend on a different way to calculate location not only on satellites but also it uses WiFi networks and nearby devices. I prefer also you take a look to this article: http://stackoverflow.com/questions/7017069/gps-time-in-android – AMS Mar 17 '17 at 06:01