Android 定位結果的 `Location.getTime()` 不一定是衛星時間

Photo by SpaceX on Unsplash 前言 最近的專案需要取定位的時間,並用該時間判斷使用者是否有在指定時間完成任務,而業主就提出希望以定位所取得的時間做為判斷依據。 然後,就發現事情沒有想像中的那麼單純。 原來目前在 Android 的定位結果中所取得的時間,只有在定位來源為純 GPS 的情況下才會是衛星所回傳的時間。 來看看究竟是怎麼一回事吧… 先認識 Location 型別 不論是使用 Android 官方推薦的 Fused Location Provider 或是 LocationManager ,定位結果所回傳的型別都是 Location。這個型別除了基本的經緯度與海拔等資訊,其中也包含定位的時間。 通常開發上指的定位時間,我們會使用 Location.getTime() 取得 Unix epoch time,再將這串數字轉成人類可閱讀的時間格式。 Unix epoch time:從 UTC 1970 年 1 月 1 日 0 時 0 分 0 秒起至現在的總秒數,不考慮閏秒。 – Wikipedia Location.getTime() 會跟定位來源有關 從 Android Doc: Location.getTime() 的說明會發現,不同的定位結果所取得的時間來源不見得相同。 當定位結果是由 LocationManager.GPS_PROVIDER 提供的時候,時間會是由衛星所提供。如果是其他的 Provider,時間來源則不一定,不過通常會是使用裝置系統的 Unix epoch time。 There is no guarantee that different locations have times set from the same clock. Locations derived from the LocationManager#GPS_PROVIDER are guaranteed to have their time originate from the clock in use by the satellite constellation that provided the fix. Locations derived from other providers may use any clock to set their time, though it is most common to use the device’s Unix epoch time system clock (which may be incorrect). ...

Published on May 27, 2023 · 2 min · 257 words · Daniel Huang

比 Android 原生更方便的 Log 工具: Timber

本文同步發表在 HackMD & Medium Timber 是什麼 Timber 是一個以 Android Log 為基底所開發的 Logger Library,由 Jake Wharton 大神所開發。 Timber 為了解決什麼問題 1. 開發時可以留著,但發佈版本需要移除 Log 1 2 3 4 // 你可能很常看到類似這樣的寫法... if (BuildConfig.DEBUG) { Log.d(TAG, "Hello World!") } 一般來說在開發上,我們習慣使用 Android 的 Log class 來印出所需的資訊。但是當今天開發到一定的階段,程式必須發布上線時,為了資訊安全等需求,需要將這些 Log 給全部註解或移除,又或是加上 buildFlavor 或 buildType 判斷,這一切實在是太麻煩了… 2. 每次在新的類別中使用 Log 就要建一個該類別的 TAG String 1 2 3 4 5 val TAG: String = Hello::class.java.simple if (BuildConfig.DEBUG) { Log.d(TAG, "Hello World!") } 同步發表在 HackMD & Medium Timber 怎麼使用 1. Dependency 在 build.gradle 中加入以下的 Dependency。 ...

Published on May 27, 2023 · 1 min · 170 words · Daniel Huang

Android 取得即時定位 LocationManager

📢 除非有特殊需求,官方建議使用 Google Fused Location Provider API。 權限請求 AndroidManifest.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <!-- 宣告定位權限 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application> <!-- 略 --> </application> </manifest> 請求動態權限 因為定位屬於危險權限需要動態向使用者請求。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import android.Manifest import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts class MainActivity : AppCompatActivity() { // -----------------------Request Location Permission------------------------------ private val resultLauncherPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { if (it) { initLocationManager() } else { Toast.makeText(this, "請允許權限以開啟定位", Toast.LENGTH_SHORT).show() finish() } } // -----------------------------LifeCycle Event------------------------------------- override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 啟動時請求權限 resultLauncherPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION) } // -------------------------------------------------------------------------------- private fun initLocationManager() { Toast.makeText(this, "定位權限請求成功,準備初始化 LocationManager", Toast.LENGTH_SHORT).show() } } LocationManager 實例化 1 2 3 import android.location.LocationManager val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager 請求定位 Android S 以上支援 FUSED_PROVIDER,不過建議直接用 Android Fused Location Provider API,支援的系統版本比較廣。 ...

Published on May 24, 2023 · 2 min · 271 words · Daniel Huang

Git 更新版本

1. 檢查目前 Git 版本 打開命令提示字元,輸入以下指令檢查目前 Git 版本 1 git version 2. 根據版本選擇不同更新方式 2-1 早於 2.14.1 版 請到 Git 官網下載新版,解除安裝本機的 Git 後手動安裝新版。 2-2 版本介於 2.14.2 和 2.16.1 之間 打開命令提示字元,輸入以下指令更新。 1 git update 2-3 版本 2.16.1 以上 打開命令提示字元,輸入以下指令更新。 1 git update-git-for-windows 執行後會於 CMD 自動下載,不過安裝過程還是會跳 GUI 要你選擇不同安裝選項。 參考資料 Poy Chang 更新本機 Git 到最新版 文章同步發表在 HackMD

Published on April 27, 2023 · 1 min · 52 words · Daniel Huang

網站憑證在 PC 端有效但 Android 上出現無效憑證問題

問題 網站更換憑證後在電腦瀏覽器上可正常瀏覽,但手機端卻出現憑證無效的錯誤。 1 javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 原因:缺乏中繼憑證 👀 以下截自 【茶包射手日記】網站憑證無效案例分析 這篇問題的確單純就是沒裝中繼 CA 憑證,而這問題用PC瀏覽器是測不出來的,因為PC瀏覽器在缺少中繼憑證時,會從憑證的擴充欄位>授權資訊存取>憑證授權單位簽發者中的網址,自動下載中繼憑證,所以不會有問題。但PC瀏覽器以外的client如手機版瀏覽器、curl、寫程式連線等都沒有這個自動下載。 另外 SSLLab 其實會指出這個問題,在Additional Certificates (if supplied)區塊就會列出server提供了哪些憑證,如果有缺少中繼憑證問題也會顯示Chain issues: Incomplete 簡單來說就是: 伺服器端沒提供中繼憑證,導致憑證無效。 PC 上的瀏覽器因為會自動從憑證授權單位自動下載中繼憑證,所以不會有問題。 其他類型的 Client 沒有這個自動下載的機制,所以會有問題。 驗證問題 嘗試手動將中繼憑證加入手機中,確認可以正常瀏覽。 使用 What’s My Chain Cert 憑證設定檢查網站,比對兩個網址的設定,也確實異常的那個網站是有錯誤的。 解決方案 很簡單,伺服器端修正憑證設定即可。 補充案例 2023/05/30 App 下載離線圖資失敗,經查測後發現一樣是在瀏覽器上可正常連線,但是 App 無法連線。 使用 What’s My Chain Cert 憑證設定檢查網站 檢查,確認是 Misconfigured。 改用 SSLChecker 憑證設定檢查網站 ,更明確指出斷在哪裡。 伺服器端匯入中繼憑證後,使用 SSLShopper SSL Checker 檢查通過。 參考資料 StackOverflow: Trust Anchor not found for Android SSL Connection 【茶包射手日記】網站憑證無效案例分析 中繼憑證設定遺失問題 【第2代通用憑證管理中心新舊中繼CA憑證差異說明】 什麼是中繼憑證 憑證檢查網站 What’sMyChainCert 憑證設定檢查網站 SSLChecker 憑證設定檢查網站 SSLShopper SSL Checker

Published on October 26, 2022 · 1 min · 89 words · Daniel Huang