am 这个命令可以帮助我们直接启动activity、service及广播。
adb shell am 可以查看命令的详细说明。
usage: am [subcommand] [options]
usage: am start [-D] [-W] [-P ] [--start-profiler ]
[--sampling INTERVAL] [-R COUNT] [-S] [--opengl-trace]
[--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]
am profile stop [--user current] []
am dumpheap [--user current] [-n]
am set-debug-app [-w] [--persistent]
am clear-debug-app
am monitor [--gdb ]
am hang [--allow-restart]
am restart
am idle-maintenance
am screen-compat [on|off]
am to-uri [INTENT]
am to-intent-uri [INTENT]
am to-app-uri [INTENT]
am switch-user
am start-user
am stop-user
am stack start
am stack movetask [true|false]
am stack resize
am stack list
am stack info
am lock-task
am lock-task stop
am get-config
am start: start an Activity. Options are:
-D: enable 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
--opengl-trace: enable tracing of OpenGL functions
--user | current: Specify which user to run as; if not
specified then run as the current user.
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 /. 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 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 bug-report: request bug report generation; will launch UI
when done to select where it should be delivered.
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 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.
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 list: list all of the activity stacks and their sizes.
am stack info: display the information about activity stack .
am lock-task: bring to the front and don't allow other tasks to run
am get-config: retrieve the configuration and any recent configurations
of the device
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 "\,")
[-n ] [-p ] [-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]
[--selector]
[ | | ]
1、首先理解一下这几个概念的意思,参考资料
活动(Activity) - 用于表现功能
服务(Service) - 相当于后台运行的 Activity
广播(Broadcast) - 用于发送广播
广播接收器(BroadcastReceiver) - 用于接收广播
Intent - 用于连接以上各个组件,并在其间传递消息
2、Intent.Action相关知识
am start 启动应用
格式:adb shell am start 包名/.类名
或 adb shell am start 包名/类的全名
后面跟的参数的意思分别是:
参数名 | 解释 |
---|---|
-a | 传入动作 |
-n | 传入组件(package) |
-t | 传入类型 |
-d | 传入数据 |
1.拨打一个电话:
`am start -a android.intent.action.CALL -d tel:10086`
2.打开一个网页:
am start -a android.intent.action.VIEW -d http://www.baidu.com
3.打开音乐播放器:
adb shell am start -n com.android.bbkmusic/.WidgetToTrackActivity
4.启动一个服务:
am startservice <服务名称>
例如:am startservice -n com.android.music/com.android.music.MediaPlaybackService
或者 am startservice -a com.smz.myservice
5.发送一个广播:
am broadcast -a <广播动作>
例如: am broadcast -a com.smz.mybroadcast
6.adb shell am monitor 监控 crash 与 ANR
7.am force-stop , 后跟包名,结束应用
值得注意的是,不同手机的相同类型应用,如相机,因为他们的package都可能是不一样的,所以要把package提取出来,再抽离为公共的方法。