新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > android4.4中自動(dòng)更新時(shí)間機(jī)制

android4.4中自動(dòng)更新時(shí)間機(jī)制

作者: 時(shí)間:2016-09-12 來(lái)源:網(wǎng)絡(luò) 收藏

名詞解釋?zhuān)?/p>本文引用地址:http://butianyuan.cn/article/201609/304655.htm

NITZ:NITZ(Network Identity and Time Zone)或網(wǎng)絡(luò)標(biāo)識(shí)和時(shí)區(qū),是一種用于自動(dòng)配置本地的時(shí)間和日期的機(jī)制,同時(shí)也通過(guò)無(wú)線網(wǎng)向移動(dòng)設(shè)備提供運(yùn)營(yíng)商信息。NITZ是自從PHASE 2+ RELEASE 96 的GSM中的可選功能,經(jīng)常被用來(lái)自動(dòng)更新移動(dòng)電話的系統(tǒng)時(shí)鐘。

NTP:NTP(Network Time Protocol)提供準(zhǔn)確時(shí)間,首先要有準(zhǔn)確的時(shí)間來(lái)源,這一時(shí)間應(yīng)該是國(guó)際標(biāo)準(zhǔn)時(shí)間UTC。 NTP獲得UTC的時(shí)間來(lái)源可以是原子鐘、天文臺(tái)、衛(wèi)星,也可以從Internet上獲取。這樣就有了準(zhǔn)確而可靠的時(shí)間源。時(shí)間按NTP服務(wù)器的等級(jí)傳播。

1.在setting中勾選“自動(dòng)確定時(shí)間和日期”,“自動(dòng)確定時(shí)區(qū)”后只是對(duì)key值為AUTO_TIME和AUTO_TIME_ZONE的Preference進(jìn)行了賦值.

源碼路徑:packages/apps/Settings/src/com/android/settings/DateTimeSettings.java

2.在/frameworks/base/services/java/com/android/server/NetworkTimeUpdateService中對(duì)上述的key值進(jìn)行了監(jiān)聽(tīng),在檢測(cè)到key值改變的時(shí)候,就會(huì)發(fā)送消息mHandler.obtainMessage(mMsg).sendToTarget();

10void observe(Context context) {

ContentResolver resolver = context.getContentResolver();

resolver.registerContentObserver(Settings.Global.getUriFor(Settings.Global.AUTO_TIME),

false, this);

}

@Override

public void onChange(boolean selfChange) {

mHandler.obtainMessage(mMsg).sendToTarget();

}

handler接到消息后進(jìn)行消息處理調(diào)用onPollNetworkTime(msg.what),發(fā)現(xiàn)無(wú)論是數(shù)據(jù)或者是wifi下都是調(diào)用該方法進(jìn)行更新

9public void handleMessage(Message msg) {

switch (msg.what) {

case EVENT_AUTO_TIME_CHANGED:

case EVENT_POLL_NETWORK_TIME:

case EVENT_NETWORK_CONNECTED:

onPollNetworkTime(msg.what);

break;

}

}

3.在onPollNetworkTime方法中先判斷是否勾選“自動(dòng)更新時(shí)間”,如果沒(méi)勾選直接退出,如果勾選了再看,如果更新的NITZ時(shí)間不為NOT_SET(-1),且更新間隔小于mPollingIntervalMs,mPollingIntervalMs=24小時(shí),那么就直接更新NITZ的時(shí)間,否則用NTP同步時(shí)間。

6// If NITZ time was received less than mPollingIntervalMs time ago,

// no need to sync to NTP.

if (mNitzTimeSetTime != NOT_SET refTime - mNitzTimeSetTime mPollingIntervalMs) {

resetAlarm(mPollingIntervalMs);

return;

}

11final long ntp = mTime.currentTimeMillis();

mTryAgainCounter = 0;

// If the clock is more than N seconds off or this is the first time it's been

// fetched since boot, set the current time.

if (Math.abs(ntp - currentTime) > mTimeErrorThresholdMs

|| mLastNtpFetchTime == NOT_SET) {

// Set the system time

......

if (ntp / 1000 Integer.MAX_VALUE) {

SystemClock.setCurrentTimeMillis(ntp);

}

當(dāng)從時(shí)間服務(wù)器上獲取的NTP時(shí)間和當(dāng)前時(shí)間之差的絕對(duì)值大于一個(gè)閥值,系統(tǒng)認(rèn)為當(dāng)前時(shí)間錯(cuò)誤,需要更新時(shí)間。

總結(jié):

如果時(shí)間自動(dòng)同步選項(xiàng)未勾選,直接返回;

如果NITZ已同步且上次NITZ同步未超過(guò)24小時(shí),則設(shè)置定時(shí)器24小時(shí)后再觸發(fā)同步,即廣播NetworkTimeUpdateService.ACTION_POLL;

如果NTP上次成功同步超過(guò)24小時(shí)或用戶(hù)勾選自動(dòng)同步選項(xiàng),則進(jìn)行下面的NTP同步,否則同上設(shè)置定時(shí)器24小時(shí)后再觸發(fā)同步;

如果上次NTP成功同步超過(guò)24小時(shí),則發(fā)起同步mTime.forceRefresh();

如果同步成功,獲取此刻N(yùn)TP時(shí)間ntp=mTime.currentTimeMillis();

如果同步時(shí)間與當(dāng)前本機(jī)時(shí)間誤差超過(guò)指定值閥值,則把ntp設(shè)置為本機(jī)時(shí)間SystemClock.setCurrentTimeMillis(ntp)



關(guān)鍵詞:

評(píng)論


相關(guān)推薦

技術(shù)專(zhuān)區(qū)

關(guān)閉