I'm trying to get GPS time in my flutter application, I'm able to get the device current time but as it can be changed in phone's setting and can't use Network provided time NTP because I'm using my application offline. Is there any flutter library from which I can get GPS date and Time (Not device time, satellite date and time) or any other solution to my problem?
I haven't tested this yet, but the location package appears to provide the time in the LocationData object: https://github.com/Lyokone/flutterlocation
Here's the time field:
https://github.com/Lyokone/flutterlocation/blob/ab79cba664284ee44ed3f7076c1c2b3038187afc/packages/location_platform_interface/lib/src/types.dart#L81
Note that it is nullable and I've read that time data isn't available in all locations. You may want to combine this with an NTP offset solution that you can calculate while online for use offline.
Related
Actually, in my application, some login expiry time is there but if the user changes device time, date settings it is not working properly, I wanted to get network time using NTP. For this, I am hitting google.com, but this is also not the proper way as per location coordinates. So is there any way to get accurate time based on location using NTP
https://developers.google.com/time/
No, NTP does not use location specific time (timezones). It uses UTC.
I am trying to get time from the device but it returns incorrect time if I change my timezone (Because of the timezone). Is there any way to fetch timezone from the internet so that If a user changes its device timezone it should show the actual real time.
BTW I am using the true time library to get real UTC time.
You can use a publicly available API for this. I use http://ip-api.com/json go thorugh documention before implementation. However if you want you can use other APIs. As I am sure there are many.
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.
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.
For some date-based calculation I needed today's correct date, for which I enabled Automatic date and time option in Date and Time Settings and this gives me the correct time and date.
I want to know how Android gets the correct date and time, even though I have not enabled any internet accessibility (WiFi, SIM Data).
Also look at this: There is no response from requestLocationUpdates
Please give me some reference on this.
It capture date and time from the network provider. not only android lots of other phone can do the same.
Autometic date time is provided be the network provide (exmp: vodafone,airtel etc etc ).