編輯:關於 Android 程式設計
對開發者來說 adb 一點都不陌生。而手機連上 adb 之後,am 指令能做的事多到出乎意料——從撥電話、開網頁、啟動 Activity,到發廣播、殺行程、監控效能,全都能用一行命令搞定。
(1)撥打 10086:
adb shell am start -a android.intent.action.CALL -d tel:10086
(2)開啟某個網站:
adb shell am start -a android.intent.action.VIEW -d https://gityuan.com
(3)啟動指定的 Activity,並帶入 extra 參數。假設套件名為 com.yuanhh.app、主 Activity 為 .MainActivity,要傳入 key 為 website、value 為 gityuan.com 的字串:
am start -n com.yuanhh.app/.MainActivity -es website gityuan.com
同樣的功能若用 Java 撰寫,雖不複雜但起碼需要一個完整的 Android 環境;透過 adb 只要在視窗裡貼上一行命令就完成了。這正是 am 指令在開發除錯上的價值。
am [subcommand] [options]
| 指令 | 功能 | 對應實作 |
|---|---|---|
am start [options] <INTENT> | 啟動 Activity | startActivityAsUser |
am startservice <INTENT> | 啟動 Service | startService |
am stopservice <INTENT> | 停止 Service | stopService |
am broadcast <INTENT> | 發送廣播 | broadcastIntent |
am kill <PACKAGE> | 殺指定背景行程 | killBackgroundProcesses |
am kill-all | 殺所有背景行程 | killAllBackgroundProcesses |
am force-stop <PACKAGE> | 強制停止行程 | forceStopPackage |
am hang | 讓系統假死 | hang |
am restart | 重啟 | restart |
am bug-report | 產生 bugreport | requestBugReport |
am dumpheap <pid> <file> | 把指定行程的堆資訊輸出到檔案 | dumpheap |
am send-trim-memory <pid> <level> | 收緊行程記憶體 | setProcessMemoryTrimLevel |
am monitor | 監控元件啟動 | MyActivityController.run |
am 指令的實作在 Am.java,最終幾乎都轉手呼叫 ActivityManagerService 的對應方法(am monitor 是唯一例外)。例如 am start -a android.intent.action.VIEW -d https://gityuan.com 最終由 AMS 的 startActivityAsUser() 完成;am kill-all 則由 AMS 的 killBackgroundProcesses() 實作。
-D:開啟除錯功能。-W:等待 App 啟動完成才返回。-R <COUNT>:重複啟動 Activity COUNT 次(測啟動效能很好用)。-S:啟動前先呼叫 forceStopPackage() 強制停止該 App。--opengl-trace:擷取 OpenGL 函式的 trace。--user <USER_ID> | current:指定以哪個使用者身分執行,預設為目前使用者。--start-profiler <FILE>:啟動 profiler 並輸出結果到 FILE。-P <FILE>:類似上者,但 App 進入 idle 狀態時即停止 profiling。--sampling INTERVAL:設定 profiler 取樣間隔,單位為毫秒。實作上,帶 -W 時走 startActivityAndWait(),否則走 startActivityAsUser()。
am send-trim-memory <pid> <level>
例如向 pid=12345 的行程發出 RUNNING_LOW 等級的收緊指令:
am send-trim-memory 12345 RUNNING_LOW
level 的可選值有:HIDDEN、RUNNING_MODERATE、BACKGROUND、RUNNING_LOW、MODERATE、RUNNING_CRITICAL、COMPLETE。
startservice、stopservice、broadcast、kill、profile start/stop、dumpheap 等子指令都支援 --user <USER_ID>。目前絕大多數手機仍是單使用者模式,可以忽略此參數。例如啟動 uid 為 10010 的使用者下的指定服務:
am startservice --user 10010
Intent 的參數眾多,以下分為常用參數、Extra 參數、Flags 參數三類。
-a <ACTION>:指定 action,實作為 Intent.setAction()。-n <COMPONENT>:指定元件名,格式為「{套件名}/.{主Activity名}」,實作為 Intent.setComponent()。-d <DATA_URI>:指定 Intent 的 data URI。-t <MIME_TYPE>:指定 MIME Type。-c <CATEGORY>:加入 category,可重複使用,實作為 Intent.addCategory()。-p <PACKAGE>:指定套件名,實作為 Intent.setPackage()。-f <FLAGS>:加入 flags,實作為 Intent.setFlags(int),緊接的參數必須是整數。實例:
am start -a android.intent.action.VIEW am start -n com.yuanhh.app/.MainActivity am start -d content://contacts/people/1 am start -t image/png am start -c android.intent.category.APP_CONTACTS
| 參數 | 型別 |
|---|---|
-e / -es | String |
-esn | String(null) |
-ez | boolean |
-ei | int |
-el | long |
-ef | float |
-eu | uri |
-ecn | component |
命名規則很直白:es 就是 Extra String 的縮寫。例如:
am start -n com.yuanhh.app/.MainActivity -es website gityuan.com
此處 -es website gityuan.com 等同於 Intent.putExtra("website", "gityuan.com")。
| 參數 | 陣列型別 |
|---|---|
-esa | String[] |
-eia | int[] |
-ela | long[] |
-efa | float[] |
多個值以逗號分隔:
am start -n com.yuanhh.app/.MainActivity -ela weekday 1,2,3,4,5
等同於 Intent.putExtra("weekday", new int[]{1,2,3,4,5})。
| 參數 | List 元素型別 |
|---|---|
-esal | String |
-eial | int |
-elal | long |
-efal | float |
am start -n com.yuanhh.app/.MainActivity -efal nums 1.2,2.2
此處會先組出 ArrayList 再放入 Intent 的第二個參數。
前面提過 -f <FLAGS> 是透過 Intent.setFlags(int) 設定,會覆蓋原有 flags;本節這些參數則是透過 Intent.addFlags(int),屬於追加。可用的 flags 包括:
--grant-read-uri-permission --grant-write-uri-permission --grant-persistable-uri-permission --grant-prefix-uri-permission --debug-log-resolution --exclude-stopped-packages --include-stopped-packages --activity-brought-to-front --activity-clear-top --activity-clear-when-task-reset --activity-exclude-from-recents --activity-launched-from-history --activity-multiple-task --activity-no-animation --activity-no-history --activity-no-user-action --activity-previous-is-top --activity-reorder-to-front --activity-reset-task-if-needed --activity-single-top --activity-clear-task --activity-task-on-home --receiver-registered-only --receiver-replace-pending
例如發送 action 為 broadcast.demo 的廣播,並排除被 force-stop 的應用接收:
am broadcast -a broadcast.demo --exclude-stopped-packages
測冷啟動時間:am start -W -S -n 套件/.MainActivity。-S 先強制停止確保是冷啟動,-W 則會印出 ThisTime、TotalTime、WaitTime,是量測啟動效能最省事的方法。
反覆重現問題:am start -R 20 -n 套件/.MainActivity,快速連續開啟 20 次,容易逼出只在特定時序下出現的 bug。
驗證 singleTop/singleTask:先用 am 啟動 Activity,再執行一次同樣的指令,觀察是走 onNewIntent 還是重新 onCreate,就能確認 launchMode 設定是否生效。
把 am 用熟之後,很多原本需要寫測試程式或手動操作才能重現的場景,都能壓縮成一行命令,除錯效率會明顯提升。
Android 自定義 View 進階:Path 常用方法與填充模式
Path 是 Android 自定義 View 中非常重要的繪圖工具,常用於繪製線段、曲線、封閉圖形與複雜路徑。本文整理 Path 的常用方法、相對座標、填充模式與布林運
小米手環能配其他品牌手機嗎?支援清單與功能差異說明
「小米手環是不是只有小米手機才能用?」這大概是它被問最多的問題。答案很明確:不是。下面把相容條件與功能差異講清楚。 一、非小米手機能用嗎? 任何品牌的手機都能配小米手環,
Android 自學筆記(二):我的第一個 HelloWorld
環境裝好了,廢話不多說,直接開工。 建立專案 打開 Eclipse,依序點選 File → New → Android Application Project: 接著就
自定義 Android 窗口標題欄樣式的實現
Android 默認的窗口標題欄(Window Title)在很多時候不合用:高度不夠、配色與界面不協調、或者只是想換個視覺風格。當需求僅停留在樣式層面時,不必寫自定義