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.
Related
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.
This is my first time to ask here on Stackoverflow and I am apparently having a hard time on this one. How could I identify if the user changed the device's date in order to activate a date-based code actions?
I really need to compare the actual date and device date without internet connection.
For example:
When I click this button, it would only open in August 29. Changing the date would allow me to access the function of the button.
You can use a Network Time Protocol. Which provides the network time and date so it can't be tricked by the user changing the phone date.
Google has one open source at: This link
EDIT
Which provides this code as sample:
SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
}
where it puts time.foo.com you should put time.google.com. You might need a timeout in milliseconds to add to the requestTime(host,timeout_millis) method.
And for NTP server you can use
time.google.com
Provided Here
If you can make sure that when your app is installed it has correct time then, you may implement a listener to know manual clock change and then do what you want to do. Reference: Is there a way to detect when the user has changed the clock time on their device?.
There are 2 more options, one to get time using GPS and other is to get time from Network. Not too sure about the network, it is something NTP related stuff will explore when I get a chance. Let me know about your implementation.
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 need two devices to have exact same time.
I want to use GPS to get time. It can be done on iOS and Android devices.
Can i be sure that they have exact same time in milliseconds?
No you cannot do that easily.
ios overrides the gps time. If a user adds one hour, and disables auto time sync,
the time is then offset. It will not work that way. If auto time sync is enabled it should work.
However you could query via internet the correct time using a time service, and do the same that NTP do. (network time protocoll). However that looks not to easy.
As an intermediate approach, I would check whether the system time relates to the network time. If yes, one can asume that auto time sync is enabled.
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 ).