Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the "network" time, (from the "Automatic" setting called "Use network-provided values"), NOT the time on the phone?

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?

enter image description here

like image 810
Waza_Be Avatar asked Nov 08 '11 11:11

Waza_Be


People also ask

How to get network time in android?

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.

What is network provided time?

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.


2 Answers

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 ServiceStateTrackers: 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 ServiceStateTrackers implementations, catch the intent raised by the framework (I suppose), and add a getter to mSavedTime.

like image 157
rds Avatar answered Sep 24 '22 18:09

rds


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() 
like image 28
pretty angela Avatar answered Sep 25 '22 18:09

pretty angela