Is it posible to get the current time from GPS - android

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?

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).

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.

Related

Android - Get current time without dependency on device's clock

I've noticed that System.currentTimeMillis() time is device dependent. If I change the time on the device's clock, this method will return a different answer.
For example: If the real time now is 10:00, and I change the clock on my device to 9:30, then System.currentTimeMillis() will return the 9:30 time (in milliseconds..).
I've also tried this answer and some other answers, but didn't find anything useful.
I should state that my app works mostly offline.
Is there a way to get the real current time (device independent) without external API?
If it were not for the 'offline' part, I'd have suggested to use a time server, but given that your app is offline most of the time that might not be a good solution.
If you don't need the actual time but just a time that cannot be messed with, you can use SystemClock.elapsedRealtime() which gives you the time since the device last booted.
You could also combine time server and SystemClock.elapsedRealtime(): Fetch the time from timer server once (e.g. after bootup) and from then on add elapsedRealtime() to that initial value (minus the elapsedRealtime value of when you get the timerserver value).
If you use the GPS location provider, getTime() will return the UTC time derived from the GPS signal, rather than the device time. The GPS location provider can work offline - but it will be much slower to obtain a fix compared to being online when it can access the A-GPS info.

Get the real time passed on Android

I'm developing an application, which will have a custom yearly subscription license. I need to know exactly how much time has passed. The user could keep the device offline, therefore I can't check the time through internet. The user could turn back the clock, therefore I can't be sure of really passed time. Is there a way to get the real time elapsed?
I think you can use System. nanoTime(), which can help you measure an absolute elapsed time (as opposed to System.currentMillis() which will be adjusted if the system clock is changed).
See the nanoTime and currentMillis javadocs for more information.
ps: I have not tested it.
you can have a preference or database that need to be stored the time when user install the application ... and you will always compare the time passed with the difference between stored time - current system time ........
or may be make a service to get network time

Setting an 'expiry' time of a GPS Location

I'm doing a camera application that will automatically geo-tag each picture taken. Using
LocationManager.requestLocationUpdates() //simply to get the GPS running
LocationManager.getLastKnownLocation() //for each picture, to get coordinates
is mostly enough to simply pluck out coordinates from the phone as fast as possible, since it is still a camera application and you don't want to waste time getting GPS data.
(Assume using GPS_PROVIDER only, not NETWORK_PROVIDER pls)
However the issue will be if the user enters a tunnel for example and the phone is unable to continue getting updates. 10 minutes later when getLastKnownLocation() is called, I will get an outdated location.
I want to find a way in which I can set an 'expiry' time on the Location, depending on individual comfort level.
In other words, keep using getLastKnownLocation(), but if the data is (for example) more than 5 minutes old, don't bother with it, and I rather have a 'null' location. if the data is 2 minutes old however, that still is fine
You can implement this yourself. There's nothing in the underlying API that includes this, but it's perfectly fine to stash the time that you initiated the request (or got the last one) in your app, perhaps in a SharedPreferences. You also have to be weary of things like how fast the user is moving, etc.., so in general you might need to put a fair amount of thought effort into determining how you want to do this. There's not any way (in the API) to find out how fresh the call to .getLastLocation() will get you. For a few minutes it's probably best to employ the technique I mentioned. For a lot longer you might want to consider something like an AlarmManager (which you should be using anyway if your app runs on a timescale that is that long). You seem to be using .getLastLocation more than I do, however. I often simply just request updates and then "put the dot down" whenever I get the first update. In general I've found that .getLastKnownLocation() isn't really all that reliable, but maybe that's just because I don't use apps with my location very often.
Managed to find out that each Location returned had a time variable
long time_a = my_loc.getTime();
We can use this to compare with
Calendar cal = Calendar.getInstance();
long time_b = cal.getTimeInMillis();
long interval = time_a - time_b;
The interval is then the 'validity' of the location fix, and can be easily used for my purpose :)

Time from location fix using emulator is off

I'm writing some android code to test out the geolocation libraries and I'm running into a problem with the android emulator. I am creating a LocationListener and when LocationListener::onLocationChanged is called I check the time of the passed in Location object to compare it to the current time. I use the DDMS emulator control window to change the location to trigger the call to onLocationChanged. Here is the code that I use in the onLocationChanged method to compare the passed in location time to the current time ("location" is the location that is passed into onLocationChanged):
float accuracy = location.getAccuracy();
long curTime = System.currentTimeMillis();
float lateness = (curTime - location.getTime()) / 1000;
The problem I'm having is that the time I get from location using getTime is always way off of the current time (curTime) even though the time between when I set the location using DDMS and the time I look at it in the debugger is a matter of seconds. Usually the difference is several hours, and sometimes the time from location is several hours ahead of the current time (so the time of the location fix occurs several hours in the future?!?). Additionally, the time diffference is not consistent. The documentation for the call to Location::getTime and System.currentTimeMillies both say that the returned time is given in milliseconds since Jan 1 1970 UTC, so it shouldn't be an issue of using different time zones. Is this a known bug with the emulator or is there something I'm doing wrong? Thanks!
GPS time is actually different from time on the device. So, they can differ by a huge amount. Wiki has a nice explanation here
More information about this can be found at Android emulator's GPS location gives wrong time as well as the bug report link. Additionally Android problem finding out how recent latest GPS fix is may be worth reading as well as to why system time and GPS time won't match.

Android problem finding out how recent latest GPS fix is

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?
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:
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.
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.
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.

Categories

Resources