自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
自動偵測時間
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
自動時間偵測功能會接收來自不同來源的時間建議,選取最佳選項,然後將 Android 中的系統時鐘設為相符的時間。先前的 Android 版本提供兩種設定日期和時間的方式,可為每位使用者手動設定,或透過自動時間偵測功能設定,並透過下列任一選項設定:
telephony
會使用網路 ID 和時區 (NITZ) 電話信號。
network
會使用網路時間通訊協定 (NTP) 時間伺服器。
每個選項都需要連線至外部網路,但 Android Automotive 不一定提供這類網路。舉例來說,在部分國家/地區,部分車輛可能沒有內建電話功能。因此,當無法連上網路時,系統會提供 全球衛星導航系統 (GNSS) 時間做為系統時間來源。
即將推出的 Android 版本提供兩個額外的選項,可自動偵測及設定時間:
啟用自動時間偵測功能
如要啟用自動時間偵測功能,請務必依序選取「設定」>「日期和時間」>「自動判斷日期和時間」:

圖 1. 選取「自動判斷日期和時間」
如要指定自動時間偵測功能應納入哪些時間來源,以及這些時間來源的優先順序,您必須修改裝置的資源設定檔 core/res/res/values/config.xml
:
<!-- Specifies priority of automatic time sources. Suggestions from higher entries in the list
take precedence over lower ones. See com.android.server.timedetector.TimeDetectorStrategy for
available sources. -->
<string-array name="config_autoTimeSourcesPriority">
<item>telephony</item>
<item>network</item>
</string-array>
在這個範例中,系統會在自動時間偵測中考量 telephony
和 network
,並將 telephony
時間建議優先於 network
時間建議。
一般來說,如果建議無效或過時,系統會忽略來自優先順序較高來源的建議。此外,如果最高優先順序的有效建議與裝置目前的系統時鐘時間相差不超過幾秒 (預設值為兩 (2) 秒),系統就不會變更時間。
較短的時間限制
Android 12 提供新的較低時間限制,可用於驗證時間建議。在推出這項功能之前,自動時間偵測功能不會驗證建議的傳入世界標準時間。使用這項功能時,系統會捨棄低限值前經過的時間。
下限值是根據建構時間戳記衍生的日期決定。這項功能的運作原理是,在建構系統映像檔之前,系統無法產生有效時間。Android 不會強制執行上限。
GNSS 時間建議
gnss
時間來源是 Android 12 的新功能,由 GPS 訊號提供。在 telephony
和 network
無法使用時,這是可靠的時間來源。這個選項已新增至 SystemServer 中的新 GnssTimeUpdateService
,可被動監聽位置更新。收到有效位置後,GnssTimeUpdateService
會向 TimeDetectorService
提出建議,後者再決定是否應更新系統時鐘。
根據預設,gnss
時間來源在 AOSP 中未啟用,因此必須由合作夥伴啟用:
<!-- Specifies priority of automatic time sources. Suggestions from higher entries in the list
take precedence over lower ones.
See com.android.server.timedetector.TimeDetectorStrategy for available sources. -->
<string-array name="config_autoTimeSourcesPriority">
<item>telephony</item>
<item>network</item>
<item>gnss</item>
</string-array>
<!-- Enables the GnssTimeUpdate service. This is the global switch for enabling Gnss time based
suggestions to TimeDetector service. See also config_autoTimeSourcesPriority. -->
<bool name="config_enableGnssTimeUpdateService">true</bool>
如要啟用這項功能,請按照下列步驟操作:
- 更新
config_enableGnssTimeUpdateService
。config_enableGnssTimeUpdateService
的值必須設為 true
。
- 更新
config_autoTimeSourcesPriority
。gnss
必須加入 config_autoTimeSourcesPriority
的項目清單。gnss
在優先清單中的順序,會決定 GNSS 建議相對於其他來源值的優先順序。
對電力的影響
GnssTimeUpdateService
會被動監聽位置更新,也就是說,它不會主動開啟 GPS 耗用額外電力。因此,啟用 GNSS 來源時的耗電量微乎其微。這也表示,除非系統中的其他應用程式或服務主動要求位置更新,否則 GnssTimeUpdateService
不會取得位置更新,也不會建議 GNSS 時間。
測試
相容性測試套件 (CTS)
我們提供 CTS 測試,以便驗證 GNSS 提供的時間是否可用。詳情請參閱 LocationShellCommand.java
。
單元測試
請參閱下列檔案中的基本單元測試:
atest frameworks/base/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeUpdateServiceTest.java
手動測試
為測試這項功能,我們已在 LocationShellCommand.java
中新增指令。使用這些指令新增測試供應器,以便指定位置和相關的 GNSS 時間。GnssTimeUpdateService
會監聽這些位置更新,並定期提供建議。
注意:這些指令的介面可能會因版本而異。
# Enable Master Location Switch in the foreground user (usually user 10 on automotive).
# If you just flashed, this can be done through Setup Wizard.
adb shell cmd location set-location-enabled true --user 10
# Add GPS test provider (this usually fails the first time and will throw a SecurityException
# with "android from <some-uid> not allowed to perform MOCK_LOCATION".)
adb shell cmd location providers add-test-provider gps
# Enable mock location permissions for previous UID
adb shell appops set <uid printed in previous error> android:mock_location allow
# Add GPS test provider (Should work with no errors.)
adb shell cmd location providers add-test-provider gps
# Enable GPS test provider
adb shell cmd location providers set-test-provider-enabled gps true
# Set location with time (time can't be earlier than the limit set by the lower bound.)
adb shell cmd location providers set-test-provider-location gps --location <LATITUDE>,<LONGITUDE> --time <TIME>
外部時間建議
外部時間建議是另一種向 Android 提供自動時間建議的方式。這項新選項可讓您向 Android 提供完全自訂的時間建議,這些建議可來自各種 ECU,而這些 ECU 可使用即時時鐘、GNSS、NITZ 或任何其他時間來源的組合。
以下建議可用於 Android 12,可做為 external
時間建議的參考:
- VHAL 屬性:提供名為
EPOCH_TIME
的新 VHAL 屬性。這個屬性表示自 1970 年 1 月 1 日世界標準時間起經過的毫秒數。其值可傳遞至 Android TimeManager
,以建議新的系統時間。如需更新這項屬性的 VHAL 實作範例,請參閱下方的參考實作。
- 系統 API:TimeManager 中提供名為
suggestExternalTime()
的新方法,可為系統提供外部時間建議。如果系統已設定為考量外部時間建議 (使用設定檔中的 config_autoTimeSourcesPriority
),則系統會在沒有更高優先順序時間建議可用時,使用傳遞至此方法的時間戳記來設定系統時間。
您可以按照下列說明,實作外部時間解決方案:
- 更新資源設定檔 (
core/res/res/values/config.xml
),然後將 external
值新增至 config_autoTimeSourcesPriority
:<string-array name="config_autoTimeSourcesPriority>
<item>external</item>
<item>gnss</item>
</string-array>
這樣做可指示 Android 在設定系統時鐘時,將外部時間建議設為最高優先順序。車輛上的硬體會將時間戳記建議寫入新的 EPOCH_TIME
VHAL 屬性。
- 供應商提供的應用程式會讀取這項屬性並呼叫
TimeManager.suggestExternal()
。接著,Android 可以使用提供的時間戳記做為新的系統時鐘值。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-30 (世界標準時間)。
[[["容易理解","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-08-30 (世界標準時間)。"],[],[],null,["*Automatic time detection* receives time suggestions from various sources, selects the\nbest option, and then sets the system clock in Android to match. Previous Android releases provided\ntwo ways to set date and time, either manually set per user or by automatic time detection, and set\nby one of these options:\n\n- `telephony` uses Network Identity and Time Zone (NITZ) telephony signals.\n- `network` uses Network Time Protocol (NTP) time servers.\n\nEach option requires connections to external networks, which aren't always available in Android\nAutomotive. For example, in some countries, some cars might not have built-in telephony. Therefore,\n*Global Satellite Navigation Systems (GNSS)* time is provided as a source of system\ntime for you to use when network connectivity is unavailable.\n\nThis upcoming Android release provides two more options to automatically detect and set time:\n\n- [gnss](./automatic_time_detection#gnss) uses Global Satellite Navigation Systems (GNSS).\n- [external](./automatic_time_detection#external) uses a VHAL property or the System API.\n\nEnable automatic time detection\n\nTo enable automatic time detection, be sure to select **Settings** \\\u003e\n**Date \\& Time** \\\u003e **Automatic Date \\& Time**:\n\n**Figure 1.**Select Automatic Date \\& Time\n\nConfigure time sources\n\nTo specify which time sources to include in automatic time detection, and the priority\nin which these time sources should be considered, you must modify the device's resource\nconfiguration file, `core/res/res/values/config.xml`: \n\n```scdoc\n\u003c!-- Specifies priority of automatic time sources. Suggestions from higher entries in the list\n take precedence over lower ones. See com.android.server.timedetector.TimeDetectorStrategy for\n available sources. --\u003e\n\u003cstring-array name=\"config_autoTimeSourcesPriority\"\u003e\n \u003citem\u003etelephony\u003c/item\u003e\n \u003citem\u003enetwork\u003c/item\u003e\n\u003c/string-array\u003e\n```\n\nIn this example, `telephony` and `network` are considered in\nautomatic time detection and `telephony` time suggestions are priortized\nahead of `network` time suggestions.\n\nGenerally speaking, suggestions from a higher priority source are ignored if the suggestion\nis either invalid or if the suggestion is too old. Also, if the highest priority valid suggestion\nmatches the device's current system clock time to within several seconds (the default value\nis two (2) seconds), the time won't be changed.\n\nLower time bound\n\nAndroid 12 provides a new *lower time bound* to use when\nvalidating time suggestions. Before this feature, automatic time detection wouldn't validate the\nsuggested incoming UTC time. With this feature, times that elapse before the lower bound are\ndiscarded.\n\nThe lower bound value is determined from a date derived from the build timestamp. This works on\nthe principle that a valid time cannot occur before the system image was built. Android does not\nenforce an upper bound.\n\nGNSS time suggestions\n\nThe `gnss` time source is new to Android 12 and is provided by GPS signals. This is\na reliable source for time when `telephony` and `network` aren't available.\nThis option is added to the new `GnssTimeUpdateService` in\nSystemServer that passively listens to location updates. When a valid location is received,\n`GnssTimeUpdateService` makes a suggestion to `TimeDetectorService`, which\nthen determines if the system clock should be updated.\n\nBy default, the `gnss` time source is *not enabled* in AOSP and, therefore,\nmust be enabled by partners: \n\n```scdoc\n\u003c!-- Specifies priority of automatic time sources. Suggestions from higher entries in the list\n take precedence over lower ones.\n See com.android.server.timedetector.TimeDetectorStrategy for available sources. --\u003e\n\u003cstring-array name=\"config_autoTimeSourcesPriority\"\u003e\n \u003citem\u003etelephony\u003c/item\u003e\n \u003citem\u003enetwork\u003c/item\u003e\n \u003citem\u003egnss\u003c/item\u003e\n\u003c/string-array\u003e\n\n\u003c!-- Enables the GnssTimeUpdate service. This is the global switch for enabling Gnss time based\n suggestions to TimeDetector service. See also config_autoTimeSourcesPriority. --\u003e\n\u003cbool name=\"config_enableGnssTimeUpdateService\"\u003etrue\u003c/bool\u003e\n```\n\nTo enable this feature:\n\n1. Update `config_enableGnssTimeUpdateService`. The value for `config_enableGnssTimeUpdateService` must be set to `true`.\n2. Update `config_autoTimeSourcesPriority`. `gnss` must be added to the item list for `config_autoTimeSourcesPriority`. The position of `gnss` in the priority list determines the priority given to GNSS suggestions, with respect to values from other sources.\n\nImpact on power\n\n`GnssTimeUpdateService` listens passively to location updates, which means that it\nnever actively turns on the GPS to consume additional power. As a result, the power consumed when\nthe GNSS source is enabled is negligible. This also means that unless another app or service in the\nsystem actively requests location updates, `GnssTimeUpdateService` doesn't get a location\nupdate and suggest a GNSS time.\n\nTesting\n\nCompatibility test suite (CTS)\n\nA CTS test is provided to verify that a GNSS-provided time is available. For details, see\n[LocationShellCommand.java](https://android.googlesource.com/platform/frameworks/base/+/android16-release/services/core/java/com/android/server/location/LocationShellCommand.java).\n\nUnit tests\n\nSee the basic unit tests in the following file: \n\n```text\natest frameworks/base/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeUpdateServiceTest.java\n```\n\nManual tests\n\nTo test this feature, new commands have been added to\n[LocationShellCommand.java](https://android.googlesource.com/platform/frameworks/base/+/android16-release/services/core/java/com/android/server/location/LocationShellCommand.java). Use these commands to add test\nproviders with which you can specify a location and the associated GNSS time.\n`GnssTimeUpdateService` listens to these location updates, and periodically\nmakes suggestions.\n\n\n**Note:** The interface for these commands may change between releases. \n\n```verilog\n# Enable Master Location Switch in the foreground user (usually user 10 on automotive).\n# If you just flashed, this can be done through Setup Wizard.\nadb shell cmd location set-location-enabled true --user 10\n\n# Add GPS test provider (this usually fails the first time and will throw a SecurityException\n# with \"android from \u003csome-uid\u003e not allowed to perform MOCK_LOCATION\".)\nadb shell cmd location providers add-test-provider gps\n\n# Enable mock location permissions for previous UID\nadb shell appops set \u003cuid printed in previous error\u003e android:mock_location allow\n\n# Add GPS test provider (Should work with no errors.)\nadb shell cmd location providers add-test-provider gps\n\n# Enable GPS test provider\nadb shell cmd location providers set-test-provider-enabled gps true\n\n# Set location with time (time can't be earlier than the limit set by the lower bound.)\nadb shell cmd location providers set-test-provider-location gps --location \u003cLATITUDE\u003e,\u003cLONGITUDE\u003e --time \u003cTIME\u003e\n```\n\nExternal time suggestions\n\n*External* time suggestions are another way to provide automatic time suggestions to\nAndroid. This new options enables you to provide entirely customized time suggestions to Android,\nwhich can originate from various ECUs that, in turn, can use a combination of a real-time clock,\nGNSS, NITZ, or any other time source.\n\nThe following suggestions are available in Android 12 to consider as `external`\ntime suggestions:\n\n- **VHAL properties.** A new [VHAL property](/docs/automotive/vhal/previous/properties) named `EPOCH_TIME` is provided. This property denotes the number of milliseconds that have elapsed since 1/1/1970 UTC. Its value can be passed to the Android `TimeManager` to suggest a new system time. A sample VHAL implementation that updates this property is provided in the [reference implementation](/docs/automotive/time/automatic_time_detection#ref-imp) below.\n- **System APIs.** A new method called `suggestExternalTime()` is available in TimeManager to provide the system with an external time suggestion. If the system is configured to take external time suggestions into account (using `config_autoTimeSourcesPriority`in the configuration file), the timestamp passed to this method is used to set the system time, *if* there are no higher priority time suggestions available.\n\nYou can implement an external time solution as described below:\n\n\n1. Update the resource configuration file (`core/res/res/values/config.xml`) and then add the value `external` to `config_autoTimeSourcesPriority`: \n\n ```scdoc\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 ```\n\n Doing so instructs Android to give external time suggestions the highest priority when setting\n the system clock. Hardware on the vehicle writes a timestamp suggestion to the new\n `EPOCH_TIME`\n [VHAL property](/docs/automotive/vhal/previous/properties#vehicle-props).\n2. A vendor-provided app reads this property and calls `TimeManager.suggestExternal()`. Android can then use the provided timestamp as the new system clock value."]]