Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

am 指令用法全解:啟動元件、發送廣播、控制行程與 Intent 參數

編輯:關於 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 指令總覽

am [subcommand] [options]
指令功能對應實作
am start [options] <INTENT>啟動 ActivitystartActivityAsUser
am startservice <INTENT>啟動 ServicestartService
am stopservice <INTENT>停止 ServicestopService
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產生 bugreportrequestBugReport
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() 實作。

二、[options] 參數

2.1 啟動 Activity

  • -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()。

2.2 收緊記憶體

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。

2.3 其他

startservice、stopservice、broadcast、kill、profile start/stop、dumpheap 等子指令都支援 --user <USER_ID>。目前絕大多數手機仍是單使用者模式,可以忽略此參數。例如啟動 uid 為 10010 的使用者下的指定服務:

am startservice --user 10010

三、<INTENT> 參數

Intent 的參數眾多,以下分為常用參數、Extra 參數、Flags 參數三類。

3.1 常用參數

  • -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

3.2 Extra 參數(基本型別)

參數型別
-e / -esString
-esnString(null)
-ezboolean
-eiint
-ellong
-effloat
-euuri
-ecncomponent

命名規則很直白:es 就是 Extra String 的縮寫。例如:

am start -n com.yuanhh.app/.MainActivity -es website gityuan.com

此處 -es website gityuan.com 等同於 Intent.putExtra("website", "gityuan.com")。

3.3 Extra 參數(陣列型別)

參數陣列型別
-esaString[]
-eiaint[]
-elalong[]
-efafloat[]

多個值以逗號分隔:

am start -n com.yuanhh.app/.MainActivity -ela weekday 1,2,3,4,5

等同於 Intent.putExtra("weekday", new int[]{1,2,3,4,5})。

3.4 Extra 參數(ArrayList 型別)

參數List 元素型別
-esalString
-eialint
-elallong
-efalfloat
am start -n com.yuanhh.app/.MainActivity -efal nums 1.2,2.2

此處會先組出 ArrayList 再放入 Intent 的第二個參數。

3.5 Flags 參數

前面提過 -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 用熟之後,很多原本需要寫測試程式或手動操作才能重現的場景,都能壓縮成一行命令,除錯效率會明顯提升。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved