GNSS 時間檢測

從 Android 12 開始,Android 可以選擇使用全球導航衛星系統 (GNSS) 向time_ detector服務建議 Unix 紀元時間。 AOSP 中預設未啟用此功能。

啟用 GNSS 時間偵測後, gnss_time_update_service被動偵聽來自 GNSS 來源的位置更新,並向time_detector服務提交 GNSS 建議。然後, time_detector服務決定是否更新系統時脈以符合建議。

對用電量的影響

AOSP gnss_time_update_service被動監聽位置更新。這意味著它永遠不會主動打開 GPS 或消耗額外的電力。這也意味著,除非系統中的另一個應用程式或服務主動請求位置更新,否則gnss_time_update_service將不會取得位置更新並建議 GNSS 時間。

執行

若要啟用 GNSS 時間偵測,設備製造商必須在系統伺服器中明確啟用gnss_time_update_service

必須更新core/res/res/values/config.xml檔案中的config_enableGnssTimeUpdateServiceconfig_autoTimeSourcesPriority值才能啟用此功能。將config_enableGnssTimeUpdateService的值設為true並將gnss加入config_autoTimeSourcesPriority的專案清單。 gnss在優先順序清單中的位置決定了 GNSS 建議相對於其他來源的建議的優先順序。

以下是core/res/res/values/config.xml檔案範例,啟用了 GNSS 時間偵測,且gnss在優先順序清單中排名第三,僅次於networktelephony

<!-- 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>network</item>
        <item>telephony</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>

調試和測試

要測試 GNSS 時間偵測,可以使用adb shell cmd location指令。使用這些命令新增測試位置提供程序,您可以在其中指定位置和關聯的 GNSS 時間。 gnss_time_update_service監聽這些位置更新,並定期提出建議。

下面顯示了adb shell cmd location指令的範例:

# 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. 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 lower than the limit set by the lower bound.)
adb shell cmd location providers set-test-provider-location gps --location LATITUDE,LONGITUDE --time TIME