Android:获取原子时间 [英] Android: Get Atomic Time

查看:106
本文介绍了Android:获取原子时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Android应用程序的基本时间.我正在使用 http://hi-android.info/src/android/net/SntpClient.java.html 作为NTP客户端,以及以下用于实现NTP的代码(=来自SntpClient.java的示例代码):

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

我尝试了以下列表中的其他服务器: http://mindprod.com/jgloss/timesources.html ,但是"client.requestTime(" SERVER)"始终为"false" ...

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

我的代码:

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

我在做什么错了?

弗雷迪

推荐答案

发现我的错误!我不知道我必须在后台处理ntp任务...我用Android的AsyncTask函数解决了这个问题.感谢所有帮助我的人!

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!

这篇关于Android:获取原子时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆