Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time of NTP-Server from Android App

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!

like image 256
Marcus Avatar asked Nov 30 '25 00:11

Marcus


1 Answers

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.

like image 86
user3011455 Avatar answered Dec 02 '25 14:12

user3011455



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!