gpt4 book ai didi

java - 从 Android App 获取 NTP-Server 的时间

转载 作者:太空宇宙 更新时间:2023-11-03 12:09:19 25 4
gpt4 key购买 nike

亲爱的安卓开发者,

我正在尝试在我的应用程序中实现 sntpclient 类,但没有成功。

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

在我的代码中有以下几行:

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();
}
}

我真的不知道这种情况下网络超时是什么意思。

如果有人对我有任何想法或提示,那就太好了。

提前致谢!

最佳答案

你应该像这样把它放在 AsyncTask 中:

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);
}
}

超时:以毫秒为单位的网络超时。所以我使用 30000 表示 30 秒。

关于java - 从 Android App 获取 NTP-Server 的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16528214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com