I would like in my application to find a way to synch the date and time with something given by an external source.
I don't want to use the phone time because I might get a difference of maybe 5 minutes around real time. and 5 minutes extra or less = 10 minutes!
I have heard about time information in the GPS satellites or in Network antennas.
I have tried with System.getCurrentTime but i get the current the of the device, so, if my device is set up 5 minutes earlier, it display the wrong time.
EDIT
To make a short question: how can I get this time?
This example demonstrate about How to get current time from Network provider in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
This means that if you set the device's time manually, it will display that time irrespective of the set timezone. If you want your device to show your time zone's time, then switch “Use network-provided time” back on and set the time zone manually.
I didn't know, but found the question interesting. So I dug in the android code... Thanks open-source :)
The screen you show is DateTimeSettings
. The checkbox "Use network-provided values" is associated to the shared preference String KEY_AUTO_TIME = "auto_time";
and also to Settings.System.AUTO_TIME
This settings is observed by an observed called mAutoTimeObserver
in the 2 network ServiceStateTracker
s: GsmServiceStateTracker
and CdmaServiceStateTracker
.
Both implementations call a method called revertToNitz()
when the settings becomes true. Apparently NITZ is the equivalent of NTP in the carrier world.
Bottom line: You can set the time to the value provided by the carrier thanks to revertToNitz()
. Unfortunately, I haven't found a mechanism to get the network time. If you really need to do this, I'm afraid, you'll have to copy these ServiceStateTracker
s implementations, catch the intent raised by the framework (I suppose), and add a getter to mSavedTime
.
Get the library from http://commons.apache.org/net/download_net.cgi
//NTP server list: http://tf.nist.gov/tf-cgi/servers.cgi public static final String TIME_SERVER = "time-a.nist.gov"; public static long getCurrentNetworkTime() { NTPUDPClient timeClient = new NTPUDPClient(); InetAddress inetAddress = InetAddress.getByName(TIME_SERVER); TimeInfo timeInfo = timeClient.getTime(inetAddress); //long returnTime = timeInfo.getReturnTime(); //local device time long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); //server time Date time = new Date(returnTime); Log.d(TAG, "Time from " + TIME_SERVER + ": " + time); return returnTime; }
getReturnTime() is same as System.currentTimeMillis().
getReceiveTimeStamp() or getTransmitTimeStamp() method should be used.
You can see the difference after setting system time to 1 hour ago.
local time : System.currentTimeMillis() timeInfo.getReturnTime() timeInfo.getMessage().getOriginateTimeStamp().getTime() NTP server time : timeInfo.getMessage().getReceiveTimeStamp().getTime() timeInfo.getMessage().getTransmitTimeStamp().getTime()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With