微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 手机设计讨论 > MTK手机平台交流 > 没有网络,NTP更新时间问题

没有网络,NTP更新时间问题

时间:10-02 整理:3721RD 点击:
[DESCRIPTION]

没有任何网路(Wifi,数据连接),把系统时间改为错误时间,然后勾选“使用网络提供时间”,系统仍然可以正确更新时间。
下面的解释同时支持android KK,L,M

[SOLUTION]

这是ntp更新时间正常设计。
ntp在第一次更新时间时会把这个值保存到mCachedNtpTime,当再次通过ntp更新时间时,如果距离上次更新时间小于24小时,ntp便不会从网络上更
新时间
而是使用上次保存的mCachedNtpTime这个加上2次更新的时间间隔getCacheAge();如果更新时间间隔大于24小时,系统会从网络更新时间。
如下为具体code
NtpTrustedTime.java(php?mod=tag&id=6090" target="_blank" class="relatedlink">Frameworks\base\core\java\android\util)

///从网络更新时间
public boolean forceRefresh() {
if (mServer == null) {
// missing server, so no trusted time available
return false;
}

if (LOGD) Log.d(TAG, "forceRefresh() fROM cache miss");
final SntpCLIent client = new SntpClient();
if (client.requestTime(mServer, (int) mTimeout)) {
mHasCache = true;
mCachedNtpTime = client.getNtpTime();
mCachedNtpElapsedRealtime = client.getNtpTimeReference();
mCachedNtpCertainty = client.getRoundTripTime() / 2;
return true;
} else {
return false;
}
}

///获取上次ntp更新的时间间隔
@Override
public long getCacheAge() {
if (mHasCache) {
return SystemClock.elapsedRealtime() - mCachedNtpElapsedRealtime;
} else {
return Long.MAX_VALUE;
}
}

///获取当前系统时间(上次保存值和时间间隔之和)
@Override
public long currentTimeMillis() {
if (!mHasCache) {
throw new IllegalStateException("Missing authoritative time source");
}
if (LOGD) Log.d(TAG, "currentTimeMillis() cache hit");

// current time is age after the last ntp cache; callers who
// want fresh values will hit makeAuthoritativ
return mCachedNtpTime + getCacheAge();
}

谢谢分享,学习中。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top