转自
http://blog.csdn.net/shy1987shy/article/details/22822995
起因:有时候android盒子起来了,半天都没有获取到时间,便查看了下这部分代码;
相关文件:
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函数里,先后顺序为以下代码所示:
163 NetworkTimeUpdateService networkTimeUpdater = null;
772 try {
773 Slog.i(TAG, "NetworkTimeUpdateService");
774 networkTimeUpdater = new NetworkTimeUpdateService(context);
775 } catch (Throwable e) {
776 reportWtf("starting NetworkTimeUpdate service", e);
777 }
926 final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
1034 try {
1035 if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemReady();
1036 } catch (Throwable e) {
1037 reportWtf("making Network Time Service ready", e);
1038 }
二、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中定义为
910
911
912
913
四、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,以此优化