Get time of NTP-Server from Android App

Marcus picture Marcus · May 13, 2013 · Viewed 7.9k times · Source

Dear android developers,

I'm trying to implement the sntpclient class in my application but it didn't work.

Class: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/net/SntpClient.java

in my code I have the following lines:

public void onClickBtn(View v)
{
    SntpClient client = new SntpClient();
     if (client.requestTime("pool.ntp.org", 10)) {
         long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
         Toast.makeText(this, "Offset: " + now, Toast.LENGTH_LONG).show();
     }
} 

I really don't know what the meaning of network timeout is in this case.

It would be great, when someone has any idea or tip for me.

Thanks in advance!

Answer

user3011455 picture user3011455 · Jun 8, 2014

You should put it in AsyncTask like this :

class GetNTPAsynctask extends AsyncTask<String, Void, Boolean> {

@Override
        protected Boolean doInBackground(String... params) {
            private SntpClient sntpClient = new SntpClient();
            return sntpClient.requestTime("pool.ntp.org", 30000);
        }
}

Timeout: network timeout in milliseconds. So i use 30000 mean 30 seconds.