在 Android 12 以上版本中,Android 可選擇使用全球導航衛星系統 (GNSS) 向 time_detector
服務建議 Unix 紀元時間。這項功能在 Android 開放原始碼計畫 (AOSP) 中預設為停用。
啟用 GNSS 時間偵測功能後,gnss_time_update_service
會被動監聽 GNSS 來源的位置更新,並將 GNSS 建議提交至 time_detector
服務。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_enableGnssTimeUpdateService
和 config_autoTimeSourcesPriority
值。將 config_enableGnssTimeUpdateService
的值設為 true
,並將 gnss
新增至 config_autoTimeSourcesPriority
的項目清單。優先順序清單中 gnss
的位置,決定了系統給予 GNSS 建議的優先順序,相對於其他來源的建議。
以下是 core/res/res/values/config.xml
檔案範例,其中已啟用 GNSS 時間偵測,且 gnss
在優先順序清單中排在 network
和 telephony
之後:
<!-- 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
指令的範例:
為前景使用者啟用主要位置切換 (通常是車輛上的使用者
10
)。如果剛完成刷機,可以透過設定精靈執行這項操作。adb shell cmd location set-location-enabled true --user 10
新增 GPS 測試供應商 (通常第一次會失敗,並擲回含有
android from <SOME_UID> not allowed to perform MOCK_LOCATION
的安全性例外狀況)adb shell cmd location providers add-test-provider gps
為先前的 UID 啟用模擬位置存取權
adb shell appops set UID_PRINTED_IN_PREVIOUS_ERROR android:mock_location allow
新增 GPS 測試供應商 (應可正常運作,不會發生錯誤)
adb shell cmd location providers set-test-provider-enabled gps true
啟用 GPS 測試供應器
adb shell cmd location providers set-test-provider-enabled gps true
設定時間和地點 (時間不得低於下限設定的限制)
adb shell cmd location providers set-test-provider-location gps --location LATITUDE,LONGITUDE --time TIME