模擬系統行為:啟動組件、發送廣播、殺進程、修改顯示屬性。
adb shell am [options]
| 命令 | 用途 | 範例 |
|---|---|---|
start | 啟動 Activity | `am start -n com.pkg/.MainActivity -a android.intent.action.VIEW -d "https://example.com"` |
startservice | 啟動 Service | `am startservice -n com.pkg/.MyService` |
broadcast | 發送廣播 | `am broadcast -a com.pkg.MY_ACTION --es key "value"` |
force-stop | 強制停止套件所有進程 | `am force-stop com.pkg` |
kill | 殺死進程 (安全模式) | `am kill com.pkg` / `am kill-all` |
kill-all | 殺死所有背景進程 | `am kill-all` |
instrument | 執行測試 | `am instrument -w com.pkg.test/androidx.test.runner.AndroidJUnitRunner` |
profile start/stop | 效能分析 | `am profile start com.pkg /sdcard/trace.trace` |
set-debug-app | 設定待除錯 App | `am set-debug-app -w com.pkg` |
display-size | 修改螢幕解析度 | `am display-size 1080x1920` / `am display-size reset` |
display-density | 修改螢幕密度 | `am display-density 320` |
to-uri / to-intent-uri | Intent 轉 URI 字串 | `am to-uri -a android.intent.action.VIEW -d "https://..."` |
-a ACTION、-d DATA_URI、-t MIME_TYPE、-n COMPONENT (pkg/cls)-e|--es KEY VALUE (String)、--ei KEY VALUE (int)、--el KEY VALUE (long)、--ef KEY VALUE (float)、--eu KEY VALUE (Uri)、--ecn KEY VALUE (ComponentName)--esa KEY VAL1 VAL2 ... (String[])、--eia KEY VAL1 VAL2 ... (int[])--grant-read-uri-permission、--grant-write-uri-permission--user USER_ID|all|current:指定用戶 (多用戶/工作配置檔)。查詢/管理安裝套件、權限、功能、用戶。
adb shell pm [options]
| 命令 | 用途 | 範例 |
|---|---|---|
list packages | 列出套件 | `pm list packages -3` (第三方)、`-s` (系統)、`-d` (停用)、`-e` (啟用)、`-f` (含 APK 路徑)、`-i` (安裝者)、`-u` (含已卸載) |
list permission-groups | 權限群組 | `pm list permission-groups` |
list permissions | 權限詳情 | `pm list permissions -g -f` (分組+完整) |
list features | 裝置功能 | `pm list features` |
list libraries | 共享庫 | `pm list libraries` |
list users | 系統用戶 | `pm list users` → `UserInfo{0:Primary:13} running` |
path | APK 路徑 | `pm path com.pkg` |
install | 安裝 APK | `pm install -r -t -g /sdcard/app.apk` (-r 覆蓋、-t 測試、-g 授權) |
uninstall | 解除安裝 | `pm uninstall -k com.pkg` (-k 保留資料) |
clear | 清除資料 | `pm clear com.pkg` |
enable/disable | 啟用/停用組件 | `pm enable com.pkg/.MyReceiver` |
grant/revoke | 授權/撤銷權限 | `pm grant com.pkg android.permission.CAMERA` |
set-install-location | 預設安裝位置 | `pm set-install-location 2` (0=自動、1=內部、2=外部) |
create-user/remove-user | 建立/移除用戶 | `pm create-user "Guest"` |
get-max-users | 最大用戶數 | `pm get-max-users` |
# 完整重置 App (清資料+重啟)
pm clear com.pkg && am start -n com.pkg/.MainActivity
# 擷取 APK 到電腦
adb pull $(adb shell pm path com.pkg | cut -d: -f2) .
# 查看特定權限被哪些 App 持有
pm list packages -f | grep -i camera
# 修改螢幕解析度測試適配
am display-size 720x1280 && am display-density 240
# 恢復
am display-size reset && am display-density reset