目录
推荐使用 极客导航:极客导航(http://www.biumall.com/jike.html)
相关文件:
frameworks/base/services/java/com/android/server/SystemServer.java
frameworks/base/services/java/com/android/server/NetworkTimeUpdateService.java
frameworks/base/core/java/android/util/NtpTrustedTime.java
frameworks/base/core/java/android/net/SntpClient.java
frameworks/base/core/res/res/values/config.xml
一、frameworks/base/services/java/com/android/server/SystemServer.java
里边的ServerThread.run函数中会启用NetworkTimeUpdateService,在这个run函数里,先后顺序为以下代码所示:
NetworkTimeUpdateService networkTimeUpdater = null;
try {
Slog.i(TAG, "NetworkTimeUpdateService");
networkTimeUpdater = new NetworkTimeUpdateService(context);
} catch (Throwable e) {
reportWtf("starting NetworkTimeUpdate service", e);
}
final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
try {
if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemReady();
} catch (Throwable e) {
reportWtf("making Network Time Service ready", e);
}
二、frameworks/base/services/java/com/android/server/NetworkTimeUpdateService.java
用一个handler处理接收三种消息来触发网络时间同步
EVENT_AUTO_TIME_CHANGED
EVENT_POLL_NETWORK_TIME
EVENT_NETWORK_CONNECTED
然后调用onPollNetworkTime去判断是否要同步网络时间,层层判断以后,会调用mTime.forceRefresh();来同步网络时间,将时间保存了mTime中,然后再调用
SystemClock.setCurrentTimeMillis(ntp);来设置系统时间,long ntp = mTime.currentTimeMillis();
其中mTime = NtpTrustedTime.getInstance(context);
三、frameworks/base/core/java/android/util/NtpTrustedTime.java
forceRefresh函数里会调用SntpClient.requestTime(mServer, (int) mTimeout)来获取时间
mServer为同步时间服务器, mTimeout为请求超时时间,在frameworks/base/core/res/res/values/config.xml中定义为
<!-- Remote server that can provide NTP responses. -->
<string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</string>
<!-- Timeout to wait for NTP server response. -->
<integer name="config_ntpTimeout">20000</integer>
四、frameworks/base/core/java/android/net/SntpClient.java
跟进这个SntpClient.requestTime()函数中就能看到其使用udp协议去请求网络时间
那么到底这个网络时间同步有多不靠谱呢?
将SntpClient.java拷一份出来到Eclipse上的demo工程里,在应用里用了一下,发现真的很不稳定,如果能连接到,几乎是立马能收到回应,否则就是把超时时间调得再长也没用。
只能在frameworks/base/services/java/com/android/server/NetworkTimeUpdateService.java文件中,将同步时间间隔POLLING_INTERVAL_SHORTER_MS从一分钟改成30秒,并且将最大尝试次数TRY_AGAIN_TIMES_MAX由3改成300,以此优化
摘抄于 《android NTP时间同步》