自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
外部时间检测
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
从 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","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-03-26。"],[],[],null,["From Android 12, Android can optionally use an external\nsource of time to suggest Unix epoch times to the [`time_detector`](/docs/core/connect/time#time_detector-service) service.\nThis isn't enabled by default in AOSP.\n\nThe *external* time origin allows device manufacturers to provide completely\ncustom time suggestions to Android. After receiving suggestions from the\nexternal time origin, the `time_detector` service determines whether to update\nthe system clock using the configured [prioritization rules](/docs/core/connect/time#automatic-time).\n\nImplementation\n\nTo support external time detection, device manufacturers must implement a\ncomponent that submits external time suggestions to the `time_detector` service\nthrough 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) method in `TimeManager`. If the external origin is\nconfigured using `config_autoTimeSourcesPriority` in the configuration file and\nno higher priority time suggestions are available, Android uses the timestamp\npassed to this method to set the current Unix epoch time."]]