【Android Clock Synchronization】Android时钟同步:基于NTP协议的第三方库Apache Commons Net
Apache Commons NetApache Commons Net库实现了许多基本的客户端网络协议,其中包括NTP/SNTP。
地址:http://commons.apache.org/net/
1、创建JAR包
进入下载页,当前的最新版本是Commons Net 3.2,选择源代码下载
下载完成,解压缩.tar.gz或.zip文件
进入目录/src/main/java,打开Eclipse,将源代码拷贝到一个新建的Java Project中
删除多余的package,只留下org.commons.net,org.commons.net.util,org.commons.net.io和org.commons.net.ntp 下面的源文件
将该项目Export...为JAR File,命名为apache-commons-net-ntp-3.2.jar
2、使用
拷贝apache-commons-net-ntp-3.2.jar到Android Project下libs目录下
代码示例:
NTPUDPClient lNTPUDPClient = new NTPUDPClient();
lNTPUDPClient.setDefaultTimeout(1000);
try {
lNTPUDPClient.open();
InetAddress lInetAddress = InetAddress.getByName("pool.ntp.org");
TimeInfo lTimeInfo = lNTPUDPClient.getTime(lInetAddress);
} catch (Exception e) {
e.printStackTrace();
} finally {
lNTPUDPClient.close();
}
类NTPUDPClient
Commons Net 3.2 API文档:http://commons.apache.org/net/api-3.2/index.html
页:
[1]