android网络时间同步总结

最近看了下网络时间同步,总结一下。

整体描述:

android网络时间同步使用NITZ(Network identity and Time Zone)运营商可选服务。

由Ril层主动上报RIL_UNSOL_NITZ_TIME_RECEIVED消息;通知(notify)给CdmaServiceStateTracker和GsmServiceStateTracker;CdmaServiceStateTracker获得网络同步时间,根据DateTimeSetting中自动同步设置,决定是否同步和发送(ACTION_NETWORK_SET_TIMEZONE、ACTION_NETWORK_SET_TIME)广播。

 

其中

/** if time between NITZ updates is less than mNitzUpdateSpacing the update may be ignored. */

private static final int NITZ_UPDATE_SPACING_DEFAULT = 1000 * 60 * 10;
    private int mNitzUpdateSpacing = SystemProperties.getInt("ro.nitz_update_spacing",
            NITZ_UPDATE_SPACING_DEFAULT);

决定同步的时间间隔,如果网络主动上报间隔小于此时间间隔,则忽略该次消息

 

/** If mNitzUpdateSpacing hasn't been exceeded but update is > mNitzUpdate do the update */
    private static final int NITZ_UPDATE_DIFF_DEFAULT = 2000;
    private int mNitzUpdateDiff = SystemProperties.getInt("ro.nitz_update_diff",
            NITZ_UPDATE_DIFF_DEFAULT);

上报的NITZ同步时间与当前处理时间的默认间隔,如果实际间隔大于此间隔,则同步该次时间。

 

同步判断条件

if ((mSavedAtTime == 0) || (timeSinceLastUpdate > nitzUpdateSpacing)
                            || (Math.abs(gained) > nitzUpdateDiff)) {
                        Log.i(LOG_TAG, "NITZ: Auto updating time of day to " + c.getTime()
                                + " NITZ receive delay=" + millisSinceNitzReceived
                                + "ms gained=" + gained + "ms from " + nitz);

                        setAndBroadcastNetworkSetTime(c.getTimeInMillis());

} else {
                        Log.i(LOG_TAG, "NITZ: ignore, a previous update was "
                                + timeSinceLastUpdate + "ms ago and gained=" + gained + "ms");
                        return;
}

 

另,上层Setting App中,DateTimeSetting设置了AutoTime is True,则CdmaServiceStateTracker和GsmServiceStateTracker的监听,根据当前

if (mSavedTimeZone != null && mSavedTime != 0 && mSavedAtTime != 0) {
            setAndBroadcastNetworkSetTimeZone(mSavedTimeZone);
            setAndBroadcastNetworkSetTime(mSavedTime
                    + (SystemClock.elapsedRealtime() - mSavedAtTime));
}

广播时间和Time Zone

你可能感兴趣的:(android)