Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Get Atomic Time

I'm trying to get the atomic time for an Android app. I'm using http://hi-android.info/src/android/net/SntpClient.java.html as NTP client and the following code to implement the NTP (= sample code from SntpClient.java):

SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
    long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
}

I tried different server from this list: http://mindprod.com/jgloss/timesources.html, but the "client.requestTime("SERVER")" is always "false" ...

My code:

SntpClient client = new SntpClient();
if (client.requestTime("hera.limescope.net", 10000)) {
    long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
    textAtomicClock.setText(String.valueOf(now));
}

What am I doing wrong?

Freddi

like image 439
Freddi Avatar asked Mar 24 '26 04:03

Freddi


1 Answers

Found my mistake! I didn't know that I had to process the ntp task in the background ... I solved the problem with Androids AsyncTask function. Thanks to all who helped me!

like image 101
Freddi Avatar answered Mar 27 '26 20:03

Freddi