am命令简介:
am是一个可执行程序,全称是activity manager(还记得ActivityManager类么?)。am命令可以启动一个activity或service,发送一个BroadCast,也可以进行诸如杀进程等强大的功能。
am在Android系统中的源码位置:
frameworks\base\cmds\am\src\com\android\commands\am
am help:
在adb shell命令行,输入am或者am help,可以列出am的使用帮助信息,部分信息如下:
255|generic_x86:/ # am
Activity manager (activity) commands:
help
Print this help text.
start-activity [-D] [-N] [-W] [-P <FILE>] [--start-profiler <FILE>]
[--sampling INTERVAL] [--streaming] [-R COUNT] [-S]
[--track-allocation] [--user <USER_ID> | current] <INTENT>
Start an Activity. Options are:
-D: enable debugging
-N: enable native debugging
-W: wait for launch to complete
--start-profiler <FILE>: start profiler and send results to <FILE>
--sampling INTERVAL: use sample profiling with INTERVAL microseconds
between samples (use with --start-profiler)
--streaming: stream the profiling output to the specified file
(use with --start-profiler)
-P <FILE>: like above, but profiling stops when app goes idle
--attach-agent <agent>: attach the given agent before binding
-R: repeat the activity launch <COUNT> 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 <USER_ID> | current: Specify which user to run as; if not
specified then run as the current user.
--stack <STACK_ID>: Specify into which stack should the activity be put.
start-service [--user <USER_ID> | current] <INTENT>
Start a Service. Options are:
--user <USER_ID> | current: Specify which user to run as; if not
specified then run as the current user.
start-foreground-service [--user <USER_ID> | current] <INTENT>
am命令常用参数说明:
命令 | 功能 | 实现方法 |
---|---|---|
am start [options] | 启动Activity | startActivityAsUser |
am startservice | 启动Service | startService |
am stopservice | 停止Service | stopService |
am broadcast | 发送广播 | broadcastIntent |
am restart | 重启 | restart |
am dumpheap <pid> <file> | 进程pid的堆信息输出到file | dumpheap |
am send-trim-memory <pid> <level> | 收紧进程的内存 | setProcessMemoryTrimLevel |
am kill <PACKAGE> | 杀指定后台进程 | killBackgroundProcesses |
am kill-all | 杀所有后台进程 | killAllBackgroundProcesses |
am force-stop <PACKAGE> | 强杀进程 | forceStopPackage |
am hang | 系统卡住 | hang |
am monitor | 监控 | MyActivityController.run |
am命令举例:
(1)启动一个activity:
am start -n PackageName/PackageName.ActivityName
1. Camera(照相机)的启动方法为:
# am start -n com.android.camera/com.android.camera.Camera
2. Browser(浏览器)的启动方法为:
# am start -n com.android.browser/com.android.browser.BrowserActivity
3. 启动浏览器(打开网址) :
am start -a android.intent.action.VIEW -d http://www.163.com
(2)启动service:
adb shell am startservice -n PackageName/services name
adb shell am startservice -n com.test.myapp/.MyService
(3)发送一个广播:
am broadcast
用am发送开机广播的命令:
am broadcast -a android.intent.action.BOOT_COMPLETED
am broadcast的参数:
[-a <ACTION>]
[-d <DATA_URI>]
[-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <COMPONENT>]
[-f <FLAGS>] [<URI>]