自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
自动时间检测
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
自动时间检测会从各种时间源接收时间建议,选择最佳选项,然后将 Android 中的系统时钟设置为与之匹配。以前的 Android 版本提供了两种设置日期和时间的方式:由每位用户手动设置,或通过自动时间检测设置(借由以下两个选项之一):
telephony
:使用网络身份和时区 (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 提供了新的时间下限,可用于验证时间建议。在这项功能推出之前,自动时间检测不会验证建议的传入 UTC 时间。有了这项功能,系统会舍弃在时间下限之前经过的时间。
下限值由根据 build 时间戳得出的日期来确定。这基于以下原则:在系统映像构建之前无法发生有效时间。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>
external 时间建议
external 时间建议是向 Android 提供自动时间建议的另一种方式。借助这个新选项,您可以向 Android 提供完全自定义的时间建议,这些建议可以源自各种实时 ECU,而这些 ECU 又可以结合使用实时时钟、GNSS、NITZ 或其他任何时间源。
Android 12 提供以下建议作为 external
时间建议:
- VHAL 属性:系统提供了一个名为
EPOCH_TIME
的新 VHAL 属性。此属性表示自世界协调时间 (UTC) 1970 年 1 月 1 日以来经过的毫秒数。可以将其值传递给 Android TimeManager
来建议新的系统时间。下面的参考实现中提供了用于更新此属性的示例 VHAL 实现。
- 系统 API:TimeManager 中提供了一个名为
suggestExternalTime()
的新方法,可为系统提供外部时间建议。如果系统已配置为将 external 时间建议考虑在内(在配置文件中使用 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 在设置系统时钟时,为 external 时间建议提供最高优先级。车辆上的硬件会将时间戳建议写入新的 EPOCH_TIME
VHAL 属性中。
- 供应商提供的应用会读取此属性并调用
TimeManager.suggestExternal()
。然后,Android 可以将提供的时间戳作为新的系统时钟值。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Automatic time detection\n\n*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-------------------------------\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----------------------\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----------------\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---------------------\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\n### Impact 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\n### Testing\n\n#### Compatibility 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\n#### Unit 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\n#### Manual 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\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."]]