在 adb shell 中,您可以使用 Activity Manager (am) 工具发出命令以执行各种系统操作,如启动 Activity、强行停止进程、广播 intent、修改设备屏幕属性及其他操作。在 shell 中,此语法为:
am
您也可以直接从 adb 发出 Activity Manager 命令,无需进入远程 shell。例如:
adb shell am start -a android.intent.action.VIEW
可用的 am 命令:
usage: am [subcommand] [options]
usage: am start [-D] [-N] [-W] [-P ] [--start-profiler ]
[--sampling INTERVAL] [-R COUNT] [-S]
[--track-allocation] [--user | current]
am startservice [--user | current]
am stopservice [--user | current]
am force-stop [--user | all | current]
am kill [--user | all | current]
am kill-all
am broadcast [--user | all | current]
am instrument [-r] [-e ] [-p ] [-w]
[--user | current]
[--no-window-animation] [--abi ]
am profile start [--user current] [--sampling INTERVAL]
am profile stop [--user current] []
am dumpheap [--user current] [-n]
am set-debug-app [-w] [--persistent]
am clear-debug-app
am set-watch-heap
am clear-watch-heap
am bug-report [--progress]
am monitor [--gdb ]
am hang [--allow-restart]
am restart
am idle-maintenance
am screen-compat [on|off]
am package-importance
am to-uri [INTENT]
am to-intent-uri [INTENT]
am to-app-uri [INTENT]
am switch-user
am start-user
am unlock-user [TOKEN_HEX]
am stop-user [-w] [-f]
am stack start
am stack movetask [true|false]
am stack resize
am stack resize-animated
am stack resize-docked-stack []
am stack size-docked-stack-test: [DELAY_MS]
am stack move-top-activity-to-pinned-stack:
am stack positiontask
am stack list
am stack info
am stack remove
am task lock
am task lock stop
am task resizeable [0 (unresizeable) | 1 (crop_windows) | 2 (resizeable) | 3 (resizeable_and_pipable)]
am task resize
am task drag-task-test [DELAY_MS]
am task size-task-test [DELAY_MS]
am get-config
am suppress-resize-config-changes
am set-inactive [--user ] true|false
am get-inactive [--user ]
am send-trim-memory [--user ]
[HIDDEN|RUNNING_MODERATE|BACKGROUND|RUNNING_LOW|MODERATE|RUNNING_CRITICAL|COMPLETE]
am get-current-user
am start: start an Activity. Options are:
-D: enable debugging
-N: enable native debugging
-W: wait for launch to complete
--start-profiler : start profiler and send results to
--sampling INTERVAL: use sample profiling with INTERVAL microseconds
between samples (use with --start-profiler)
-P : like above, but profiling stops when app goes idle
-R: repeat the activity launch times. Prior to each repeat,
the top activity will be finished.
-S: force stop the target app before starting the activity
--track-allocation: enable tracking of object allocations
--user | current: Specify which user to run as; if not
specified then run as the current user.
--stack : Specify into which stack should the activity be put.
am startservice: start a Service. Options are:
--user | current: Specify which user to run as; if not
specified then run as the current user.
am stopservice: stop a Service. Options are:
--user | current: Specify which user to run as; if not
specified then run as the current user.
am force-stop: force stop everything associated with .
--user | all | current: Specify user to force stop;
all users if not specified.
am kill: Kill all processes associated with . Only kills.
processes that are safe to kill -- that is, will not impact the user
experience.
--user | all | current: Specify user whose processes to kill;
all users if not specified.
am kill-all: Kill all background processes.
am broadcast: send a broadcast Intent. Options are:
--user | all | current: Specify which user to send to; if not
specified then send to all users.
--receiver-permission : Require receiver to hold permission.
am instrument: start an Instrumentation. Typically this target
is the form / or only if there
is only one instrumentation. Options are:
-r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT). Use with
[-e perf true] to generate raw output for performance measurements.
-e : set argument to . For test runners a
common form is [-e [,...]].
-p : write profiling data to
-w: wait for instrumentation to finish before returning. Required for
test runners.
--user | current: Specify user instrumentation runs in;
current user if not specified.
--no-window-animation: turn off window animations while running.
--abi : Launch the instrumented process with the selected ABI.
This assumes that the process supports the selected ABI.
am trace-ipc: Trace IPC transactions.
start: start tracing IPC transactions.
stop: stop tracing IPC transactions and dump the results to file.
--dump-file : Specify the file the trace should be dumped to.
am profile: start and stop profiler on a process. The given argument
may be either a process name or pid. Options are:
--user | current: When supplying a process name,
specify user of process to profile; uses current user if not specified.
am dumpheap: dump the heap of a process. The given argument may
be either a process name or pid. Options are:
-n: dump native heap instead of managed heap
--user | current: When supplying a process name,
specify user of process to dump; uses current user if not specified.
am set-debug-app: set application to debug. Options are:
-w: wait for debugger when application starts
--persistent: retain this value
am clear-debug-app: clear the previously set-debug-app.
am set-watch-heap: start monitoring pss size of , if it is at or
above then a heap dump is collected for the user to report
am clear-watch-heap: clear the previously set-watch-heap.
am bug-report: request bug report generation; will launch a notification
when done to select where it should be delivered. Options are:
--progress: will launch a notification right away to show its progress.
am monitor: start monitoring for crashes or ANRs.
--gdb: start gdbserv on the given port at crash/ANR
am hang: hang the system.
--allow-restart: allow watchdog to perform normal system restart
am restart: restart the user-space system.
am idle-maintenance: perform idle maintenance now.
am screen-compat: control screen compatibility mode of .
am package-importance: print current importance of .
am to-uri: print the given Intent specification as a URI.
am to-intent-uri: print the given Intent specification as an intent: URI.
am to-app-uri: print the given Intent specification as an android-app: URI.
am switch-user: switch to put USER_ID in the foreground, starting
execution of that user if it is currently stopped.
am start-user: start USER_ID in background if it is currently stopped,
use switch-user if you want to start the user in foreground.
am stop-user: stop execution of USER_ID, not allowing it to run any
code until a later explicit start or switch to it.
-w: wait for stop-user to complete.
-f: force stop even if there are related users that cannot be stopped.
am stack start: start a new activity on using .
am stack movetask: move from its current stack to the top (true) or bottom (false) of .
am stack resize: change size and position to .
am stack resize-docked-stack: change docked stack to
and supplying temporary different task bounds indicated by
am stack size-docked-stack-test: test command for sizing docked stack by
increments from the side eft, op, ight, or ottom
applying the optional [DELAY_MS] between each step.
am stack move-top-activity-to-pinned-stack: moves the top activity from
to the pinned stack using for the
bounds of the pinned stack.
am stack positiontask: place in at
am stack list: list all of the activity stacks and their sizes.
am stack info: display the information about activity stack .
am stack remove: remove stack .
am task lock: bring to the front and don't allow other tasks to run.
am task lock stop: end the current task lock.
am task resizeable: change resizeable mode of .
0 (unresizeable) | 1 (crop_windows) | 2 (resizeable) | 3 (resizeable_and_pipable)
am task resize: makes sure is in a stack with the specified bounds.
Forces the task to be resizeable and creates a stack if no existing stack
has the specified bounds.
am task drag-task-test: test command for dragging/moving by
increments around the screen applying the optional [DELAY_MS]
between each step.
am task size-task-test: test command for sizing by increments within the screen applying the optional [DELAY_MS] between
each step.
am get-config: retrieve the configuration and any recent configurations
of the device.
am suppress-resize-config-changes: suppresses configuration changes due to
user resizing an activity/task.
am set-inactive: sets the inactive state of an app.
am get-inactive: returns the inactive state of an app.
am send-trim-memory: send a memory trim event to a .
am get-current-user: returns id of the current foreground user.
specifications include these flags and arguments:
[-a ] [-d ] [-t ]
[-c [-c ] ...]
[-e|--es ...]
[--esn ...]
[--ez ...]
[--ei ...]
[--el ...]
[--ef ...]
[--eu ...]
[--ecn ]
[--eia [, [,)
[--ela [, [,)
[--efa [, [,)
[--esa [, [,; to embed a comma into a string,
escape it using "\,")
[--f ]
[--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]
[--receiver-foreground]
[--selector]
[ | | ]
由于好几个 am
命令都可以带 Intent
参数,比如 am start
、am broadcast
等,这里就先介绍一下 Intent 参数规范。
可以参考官方文档
[-a ]
:指定 intent 的 Action(setAction()方法),如“android.intent.action.VIEW”。此指定只能声明一次。 am start -a android.intent.action.hq.TEST_AM
[-d ]
:指定 intent 的 Data(setData()方法),如“content://contacts/people/1”。此指定只能声明一次。[-t ]
:指定 intent MIME 类型,如“image/png”。此指定只能声明一次。[-c [-c ] ...]
:指定 intent 类别(addCategory(String category)),如“android.intent.category.APP_CONTACTS”。[-n ]
:指定带有包名前缀的组件名称以创建显式 intent,如“com.example.heqiang.testsomething/.commontest.OtherTestActivity”。[--f ]
:将标志添加到intent[-e|--es ...]
:添加一个 null extra。URI intent 不支持此选项。 adb shell am start -a android.intent.action.hq.TEST_AM --es TEST test
,如果有多个参数:adb shell am start -a android.intent.action.hq.TEST_AM --es TEST test --es TEST1 test1
[--esn ...]
:添加字符串数据作为键值对。 adb shell am start -a android.intent.action.hq.TEST_AM --esn TEST
[--ez ...]
:添加布尔型数据作为键值对。 adb shell am start -a android.intent.action.hq.TEST_AM --ez TEST true
[--ei ...]
:添加整数型数据作为键值对。[--el ...]
:添加长整型数据作为键值对。[--ef ...]
:添加浮点型数据作为键值对。[--eu ...]
:添加 URI 数据作为键值对。[--ecn ]
:添加组件名称,将其作为 ComponentName 对象进行转换和传递。[--eia [,:添加整数数组, 转换成Integer[]进行传递
[--eial [,:添加整数数组,转换成List进行传递
[--ela [,:添加长整型数组,转换成Long[]进行传递
[--elal [,:添加长整型数组,转换成List进行传递
[--efa [,:添加浮点型数组,转换成Float[]进行传递
[--efal [,:添加浮点型数组,转换成List进行传递
[--esa [,:添加字符串数组,转换成String[]进行传递adb shell am start -a android.intent.action.hq.TEST_AM --esa TEST a,b,c
[--esal [,:添加字符串数组,转换成List进行传递
[--grant-read-uri-permission]
:包含标志 FLAG_GRANT_READ_URI_PERMISSION。[--grant-write-uri-permission]
:包含标志 FLAG_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]
:[--receiver-foreground]
:[--selector]
:[ | | ]
:如果不受上述某一选项的限制,那么就认为是直接指定 URI、包名和组件名称。当参数不受限制时,如果参数包含一个“:”(冒号),则认为参数是一个 URI;如果参数包含一个“/”(正斜杠),则认为参数是一个组件名称;否则认为参数是一个包名。启动 intent 指定的 Activity:
am start [-D] [-N] [-W] [-P ] [--start-profiler ]
[--sampling INTERVAL] [-R COUNT] [-S]
[--track-allocation] [--user | current]
-W:等待启动完成。
Status: ok
Activity: com.example.heqiang.testsomething/.commontest.OtherTestActivity
ThisTime: 131
TotalTime: 131
WaitTime: 157
Complete
–start-profiler :启动分析器并将结果发送到 file。
启动 intent 指定的 Service:
am stopservice [--user | current]
am force-stop [--user | all | current]
强行停止与 package(应用的包名称)关联的所有应用。
终止与 package(应用的包名称)关联的所有进程。此命令仅终止可安全终止且不会影响用户体验的进程。
am kill [--user | all | current]
终止所有后台进程。
发出广播 intent。
am broadcast [--user | all | current]
am stack start
:am stack movetask [true|false]
:am stack resize
:am stack resize-animated
:am stack resize-docked-stack []
:am stack size-docked-stack-test: [DELAY_MS]
:am stack move-top-activity-to-pinned-stack:
:am stack positiontask
:am stack list
:am stack info
:am stack remove
:am task lock
:am task lock stop
:am task resizeable [0 (unresizeable) | 1 (crop_windows) | 2 (resizeable) | 3 (resizeable_and_pipable)]
:am task resize
:am task drag-task-test [DELAY_MS]
:am task size-task-test [DELAY_MS]
:发送回收内存的命令,会调用 Application 和 Activity 的 onTrimMemory(int level)
方法。
am send-trim-memory [--user ]
[HIDDEN|RUNNING_MODERATE|BACKGROUND|RUNNING_LOW|MODERATE|RUNNING_CRITICAL|COMPLETE]
adb shell am send-trim-memory com.example.heqiang.testsomething RUNNING_CRITICAL