安卓:获取来自NTP服务器的日期和时间日期、服务器、时间、NTP

由网友(安辰)分享简介:我想获得我自己的时间服务器(如192.168.1.X)的日期和时间,在我的Andr​​oid应用程序I would like to obtain the date and time from my own time server (i.e. 192.168.1.X) for the use inside my And...

我想获得我自己的时间服务器(如192.168.1.X)的日期和时间,在我的Andr​​oid应用程序

I would like to obtain the date and time from my own time server (i.e. 192.168.1.X) for the use inside my Android application

我有搜索在网上,有两种解决方法:

I have searched in the web and there are two solutions:

solution 1

solution 2

不过,我不知道是否有没有导入任何库中的任何解决方案?

However, I was wonder if there is any solution without importing any libraries?

谢谢!

推荐答案

我最终使用this 之一。

创建另一个类,然后里面添加code。然后调用类从我的主要活动取得的日期。

Create another class and then add the code inside. And then calling the class from my main activity to obtain the date.

在code是有点老了,我修改了它加入的AsyncTask ......工作正常,我...

The code is a bit old and I modified it by adding AsyncTask... works fine for me...

还有一件事,在类的用法(从什么code sugguest)给出了将String.valueOf(现)返回0日期应使用如下:

And one more thing, the usage of the class (from what the code sugguest) gives String.valueof(now) returns 0. Date should be used as below:

SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
    long now = client.getNtpTime() + SystemClock.elapsedRealtime() - 
    client.getNtpTimeReference();
    Date current = new Date(now);
    Log.i("NTP tag", current.toString());
}
阅读全文

相关推荐

最新文章