自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
外部時間偵測
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
自 Android 12 起,Android 可選擇使用外部時間來源,為 time_detector
服務建議 Unix 紀元時間。這項功能在 AOSP 中預設為停用。
外部時間來源可讓裝置製造商為 Android 提供完全自訂的時間建議。收到外部時間來源的建議後,time_detector
服務會判斷是否要使用已設定的優先順序規則更新系統時鐘。
實作
如要支援外部時間偵測,裝置製造商必須實作元件,透過系統 API 呼叫將外部時間建議提交至 time_detector
服務。
如要設定外部來源,請使用 core/res/res/values/config.xml
系統伺服器設定檔。將 external
值新增至 config_autoTimeSourcesPriority
。
以下範例會指示 Android 在設定系統時鐘時,將外部時間建議設為最高優先順序。如果沒有有效的外部時間建議,系統會使用下一個來源,在本例中為 gnss
。
<string-array name="config_autoTimeSourcesPriority">
<item>external</item>
<item>gnss</item>
</string-array>
如要向系統提供外部時間建議,請在 TimeManager
中使用 suggestExternalTime()
方法。如果外部來源是使用設定檔中的 config_autoTimeSourcesPriority
進行設定,且沒有更高優先順序的時間建議,Android 會使用傳遞至此方法的時間戳記,設定目前的 Unix 紀元時間。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# External time detection\n\nFrom Android 12, Android can optionally use an external\nsource of time to suggest Unix epoch times to the\n[`time_detector`](/docs/core/connect/time#time_detector) service. This isn't\nenabled by default in AOSP.\n\nThe *external* time origin allows device manufacturers to provide completely\ncustom time suggestions to Android. After\nreceiving suggestions from the external time origin, the `time_detector` service\ndetermines whether to update the system clock using the configured\n[prioritization rules](/docs/core/connect/time#automatic-time).\n\nImplementation\n--------------\n\nTo support external time detection, device manufacturers must implement a\ncomponent that submits external time suggestions to the `time_detector`\nservice through a system API call.\n\nTo configure the external origin, use the `core/res/res/values/config.xml`\nsystem server configuration file. Add the value `external` to\n`config_autoTimeSourcesPriority`.\n\nThe following example tells Android to give external time suggestions the\nhighest priority when setting the system clock. If there is no valid external\ntime suggestion available then the next origin, `gnss` in this case, is used. \n\n \u003cstring-array name=\"config_autoTimeSourcesPriority\"\u003e\n \u003citem\u003eexternal\u003c/item\u003e\n \u003citem\u003egnss\u003c/item\u003e\n \u003c/string-array\u003e\n\nTo provide the system with an external time suggestion, use the\n[`suggestExternalTime()`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/core/java/android/app/time/TimeManager.java?q=%22void%20suggestExternalTime%22)\nmethod in `TimeManager`. If the external origin is configured using\n`config_autoTimeSourcesPriority` in the configuration file and no higher\npriority time suggestions are available, Android uses the\ntimestamp passed to this method to set the current Unix epoch time."]]