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

没有任何网路(Wifi,数据连接),把系统时间改为错误时间,然后勾选“使用网络提供时间”,系统仍然可以正确更新时间。
下面的解释同时支持Android KK,L,M
[SOLUTION]
这是ntp更新时间正常设计。
ntp在第一次更新时间时会把这个值保存到mCachedNtpTime,当再次通过ntp更新时间时,如果距离上次更新时间小于24小时,ntp便不会从网络上更新时间
而是使用上次保存的mCachedNtpTime这个加上2次更新的时间间隔getCacheAge();如果更新时间间隔大于24小时,系统会从网络更新时间。
如下为具体code
NtpTrustedTime.java(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 makeAuthoritative() first.
        return mCachedNtpTime + getCacheAge();
    }
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值