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,支援的系統版本比較廣。 ...

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

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

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

Android CameraX Bitmap 繪製後照片方向錯誤

問題說明 在使用 CameraX API 時,如果有在相機初始時實作下列程式碼,原則上API 會根據目前手機的方向來自動轉正相片。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // 控制照片轉正 val orientationEventListener = object : OrientationEventListener(this) { override fun onOrientationChanged(orientation: Int) { val rotation: Int = when (orientation) { in 45..134 -> Surface.ROTATION_270 // 頭朝右 in 135..224 -> Surface.ROTATION_180 // 頭朝下 in 225..314 -> Surface.ROTATION_90 // 頭朝左 else -> Surface.ROTATION_0 // 頭朝上 } this@MainActivity.rotation = rotation imageCapture!!.targetRotation = rotation } } orientationEventListener.enable() 但是,當我們需要在輸出的照片上繪製文字或圖案時,就會遇到讀入的Bitmap 都是預設的橫向,而當我們需要繪製的時候,就會有長寬座標錯置的問題。 解決方式 必須把照片轉兩次。 需要轉兩次的原因在於,第一次的轉正是為了讓我們能夠根據使用者的角度,去繪製我們需要繪製的圖案或文字,但當繪製完成後,在輸出之前必須再將照片轉回原始讀入時的橫向,讓電腦端能夠用EXIF來自動轉正相片。 轉正跟轉回,可以試著拿張名片或紙自己轉轉看就會懂了。 實作的程式碼如下 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 companion object { private const val ROTATION_HEAD_UP = 0 private const val ROTATION_HEAD_LEFT = 1 private const val ROTATION_HEAD_DOWN = 2 private const val ROTATION_HEAD_RIGHT = 3 } private fun processPictureFile(photoFile: File) { // 建立 Bitmap的設定 val bitmapOptions = BitmapFactory.Options() bitmapOptions.inPurgeable = true bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565 bitmapOptions.inDither = true bitmapOptions.inMutable = true // 避免Canvas物件建立時picBitmap為immutable // 1. 旋轉照片 val mat = Matrix() when (rotation) { ROTATION_HEAD_UP -> mat.postRotate(90f) // 手機上端朝右時,照片會上下顛倒,需特別處理轉正。 ROTATION_HEAD_RIGHT -> mat.postRotate(180f) ROTATION_HEAD_DOWN -> mat.postRotate(270f) ROTATION_HEAD_LEFT -> mat.postRotate(0f) } try { // 讀入原始相片 val inputStream = ByteArrayInputStream(photoFile.readBytes()) // 原始相片轉成 Bitmap val bitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOptions) ?: return Log.d(TAG, "processPictureFile: bitmap.width ${bitmap.width}") Log.d(TAG, "processPictureFile: bitmap.height ${bitmap.height}") // 複製一份要轉方向的 Bitmap val picBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, mat, true) // 建立畫布 val canvas = Canvas(picBitmap) // 繪製在畫布上 canvas.drawBitmap(bitmap, mat, null) // TODO: 執行你需要做的繪製行為... // 設定轉回去的矩陣 val restoreMat = Matrix() when (rotation) { ROTATION_HEAD_UP -> restoreMat.postRotate(270f) ROTATION_HEAD_RIGHT -> restoreMat.postRotate(180f) ROTATION_HEAD_DOWN -> restoreMat.postRotate(90f) ROTATION_HEAD_LEFT -> restoreMat.postRotate(0f) } // 執行轉回去 val restoreBitmap = Bitmap.createBitmap(picBitmap, 0, 0, picBitmap.width, picBitmap.height, restoreMat, true) val restoreCanvas = Canvas(restoreBitmap) restoreCanvas.drawBitmap(restoreBitmap, mat, null) // TODO: 輸出... } catch (e: java.lang.Exception) { Log.e(TAG, "processPictureFile: ", e) } } 補充: 相片轉正的邏輯 對 Android 手機而言,頭朝左的水平方向(landscape),才是正常的方向,所以預設讀取的圖片都會是橫向。 因此直向拍照等方式都需要轉向。 ...

March 25, 2022 · 2 min · 361 words · Daniel Huang

Android 常用 ADB 指令

裝置 列出裝置清單 1 adb devices 取得裝置 IP 1 2 adb shell $ ip -f inet addr show wlan0 啟用裝置 port 1 adb -s {deviceId} tcpip {port} 透過Wi-Fi連結裝置 1 adb -s {deviceId} connect {ip:port} 斷開連結 1 adb disconnect 安裝apk 全新安裝 在xxxx.apk的根目錄下執行cmd 1 adb -s {deviceId} install {xxxxx.apk} 覆蓋舊版 1 adb -s {deviceId} install -r {xxxx.apk} 裝置控制 點亮螢幕 1 adb shell input keyevent KEYCODE_POWER 參考資料 Day 8 - 常用 adb 指令及實用小技巧 如何透過 adb command line 指令啟動 Android App

November 17, 2021 · 1 min · 76 words · Daniel Huang