How can I get the "network" time (from the "Automatic" option called "Use network values"), and not the time on the phone?

In my application, I would like to find a way to synchronize the date and time with something provided by an external source.

I do not want to use phone time, because I can get a difference of 5 minutes in real time. and 5 minutes extra or less = 10 minutes!

I have heard time information on GPS satellites or on network antennas.

I tried with System.getCurrentTime, but I got the current device, so if my device is configured 5 minutes earlier, it displays the wrong time.

EDIT

To ask a short question: how can I get this time?

enter image description here

+45
android time networking
Nov 08 '11 at 11:49
source share
6 answers

I did not know, but I found an interesting question. So, I dug up the android code ... Thanks open-source :)

The displayed DateTimeSettings screen. The "Use network values" checkbox is associated with the general preference String KEY_AUTO_TIME = "auto_time"; as well as Settings.System.AUTO_TIME

These settings are observed by the observable name mAutoTimeObserver in ServiceStateTracker s 2nd network: GsmServiceStateTracker and CdmaServiceStateTracker .

Both implementations call a method called revertToNitz() when the settings become true. NITZ appears to be the equivalent of NTP in the operator world.

Bottom line: you can set the time to the value provided by the carrier thanks to revertToNitz() . Unfortunately, I did not find a mechanism to get network time. If you really need to do this, I'm afraid you will have to copy these implementations of ServiceStateTracker , catch the intention raised by the framework (I suppose), and add getter to mSavedTime .

+46
Nov 21 '11 at 15:31
source share

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 similar to System.currentTimeMillis ().

Use the getReceiveTimeStamp () or getTransmitTimeStamp () method.

You can see the difference after setting the system time up 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() 
+18
Feb 06 '13 at 3:19
source share

Try this piece of code:

 String timeSettings = android.provider.Settings.System.getString( this.getContentResolver(), android.provider.Settings.System.AUTO_TIME); if (timeSettings.contentEquals("0")) { android.provider.Settings.System.putString( this.getContentResolver(), android.provider.Settings.System.AUTO_TIME, "1"); } Date now = new Date(System.currentTimeMillis()); Log.d("Date", now.toString()); 

Be sure to add permission in the manifest

 <uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
+5
Apr 16 '15 at 8:07
source share

It seemed to work for me:

 LocationManager locMan = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE); long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime(); 

Work with Android 2.2 API (level 8)

+2
May 25 '12 at 16:47
source share

NITZ is an NTP form and is sent to a mobile device via layer 3 layers or NAS. Typically, this message is considered GMM information and contains the following information:

Some media do not support this, and some support it and are configured incorrectly.

SLINE MESSAGE 3

Time: 9: 38: 49.800

Information about GMM 3GPP TS 24.008 ver 12.12.0 Rel 12 (9.4.19)

M Protocol discriminator (hexadecimal data: 8)

 (0x8) Mobility Management message for GPRS services 

Transition indicator M (hexadecimal data: 0) Value: 0 M Message type (hexadecimal data: 21) Message number: 33

O Network time zone (hexadecimal data: 4680) Time zone value: GMT + 2: 00 O Universal time and time zone (hexadecimal data: 47716070 70831580) Year: 17 Month: 06 Day: 07 Hour: 07 Minute: 38 Second: 51 Time zone value: GMT + 2: 00 O Network Daylight saving time (hexadecimal data: 490100) Daylight saving time: no adjustment

Level 3 data: 08 21 46 80 47 71 60 70 70 83 15 80 49 01 00

+1
Jun 29 '17 at 11:50
source share

the time signal is not built into the network antennas: you need to use the NTP protocol to get time on the ntp server. There are many ntp clients available as standalone executables or libraries.

The GPS signal does contain an accurate time signal, which is available with any β€œfix”.

however, if neither network nor gps is available, your only choice is to resort to phone time ... your best solution would be to use the system setting to automatically synchronize the phone time with gps or ntp, then always use the phone time.

Please note that the phone’s time, if synchronized regularly, should not differ much from the gps or ntp time. also note that forcing a user to synchronize their time can be intrusive; better ask your user if he agrees to synchronize. Finally, are you sure you absolutely need the exact time?

-one
Nov 08 2018-11-11T00:
source share



All Articles