Android AM命令使用
须志新
2023-12-01
Android AM命令使用
Android系统可以通过终端使用AM命令启动某个指定的Activity。在具体应用中可以通过AM命令来达到启动指定Activity的目的。
例如启动 Camera.apk的主activity
am start -n com.android.camera/com.android.camera.Camera
其中com.android.camera是应用Activity所在的包名,com.android.camera.Camera是指定要启动的Activity。
--------------------------
am 命令使用说明:
usage: am [subcommand] [options]
start an Activity: am start [-D] <INTENT>
-D: enable debugging
send a broadcast Intent: am broadcast <INTENT>
start an Instrumentation: am instrument [flags] <COMPONENT>
-r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
-e <NAME> <VALUE>: set argument <NAME> to <VALUE>
-p <FILE>: write profiling data to <FILE>
-w: wait for instrumentation to finish before returning
start profiling: am profile <PROCESS> start <FILE>
stop profiling: am profile <PROCESS> stop
<INTENT> specifications include these flags:
[-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>]
启动的方法为
# am start -n 包(package)名/包名.活动(activity)名称
启动的方法可以从每个应用的AndroidManifest.xml的文件中得到
--------------------------
am命令在系统中的具体应用:
Music 和 Video(音乐和视频)的启动方法为:
# am start -n com.android.music/com.android.music.MusicBrowserActivity
# am start -n com.android.music/com.android.music.VideoBrowserActivity
# am start -n com.android.music/com.android.music.MediaPlaybackActivity
Camera(照相机)的启动方法为:
# am start --user0 -n com.android.camera/com.android.camera.Camera
Browser(浏览器)的启动方法为:
# am start -n com.android.browser/com.android.browser.BrowserActivity
启动浏览器 :
am start -a android.intent.action.VIEW -d http://www.google.cn/
拨打电话 :
am start -a android.intent.action.CALL -d tel:10086
启动 google map 直接定位到北京 :
am start -a android.intent.action.VIEW geo:0,0?q=beijing
从API16开始谷歌增加了多用户模式(类似window不同用户使用,可以自行设置手机,显示则不同)
不过只能用于平板,手机仍然是一个用户,因此在Android16以上需要添加用户名 即
/*
* if(file == NULL){
//打开网页 调用am命令
execlp("am", "am", "start", "--user","0","-a", "android.intent.action.VIEW", "-d", "http://www.baidu.com", (char *) NULL);
}else{
execlp("am", "am", "start", "--user","0", "-n" , "com.itheima.cforkdemo/com.itheima.cforkdemo.MainActivity",(char *) NULL);
}
-n代表打开组件
-d代表 data
-a代表action
-----------------------------------------------------
何为am指令
Android自带的命令也有些Linux本身没有,而android特有的。例如android的am命令,am命令常见使用方法如下: am [subcommand] [options],即命令后面跟着一个子命令,因为是子命令而不是参数,所以不用加-,而后面的参数就需要加-,比如am start -a *,start在这里就是子命令,-a在这里就是参数。
直接去看下am的代码Am.Java。每次在shell环境下执行am即启动一个线程执行Am.java的主函数,这个主函数的主要实现都在run方法里面,am命令后面带的参数会当做运行时参数传递个主方法,后面会根据传入的参数不同,执行不同的条件分支。可以看到当参数的个数小于1的时候,就执行showUsage()方法,即说明am命令使用方式的方法,并且有最常用的参数intent的详细使用说明。am命令中最常用到的是start命令,下面来看下这个命令的执行过程。
代码根据第一个参数判断是否是start子命令,进入private void runStart() throws Exception方法,接着进入执行解析参数的关键方法 private Intent makeIntent(int defUser) throws URISyntaxException里面。makeIntent里面用while循环根据传入的参数一点一点的配置即将使用的intent,大的比如intent的action,data小的比如flag,参数等。回到runStart方法,首先是检查am start是否接着-S(stop)参数,如果不是就跨进程调用am服务执行命令。到这里am及其start子命令的执行过程基本介绍完了。
am指令查看
命令窗口通过adb shell 进入android 的Linux命令界面,输入am -help看到如下信息:
am指令查看
am指令使用
我们可以通过命令启动android中的Activity,Service,BroadcastReceiver 等组件
拨打一个电话:
am start -a android.intent.action.CALL -d tel:10086
这里-a表示动作,-d表述传入的数据,还有-t表示传入的类型。
打开一个网页:
am start -a android.intent.action.VIEW -d http://www.baidu.com (这里-d表示传入的data)
打开音乐播放器:
am start -a android.intent.action.MUSIC_PLAYER 或者
am start -n com.android.music/om.android.music.MusicBrowserActivity
(包名和应用名可以在Androidmanifest.xml文件查看到)
启动一个服务:
am startservice <服务名称>
例如:am startservice -n com.android.music/com.android.music.MediaPlaybackService (这里-n表示组件)
或者 am startservice -a com.smz.myservice (这里-a表示动作,就是你在Androidmanifest里定义的)
发送一个广播:
am broadcast -a <广播动作>
例如: am broadcast -a com.smz.mybroadcast
还可放在代码中操作。
char intent[1024] = {0};
sprintf(intent, "am start -a com.android.browser.ProxySelector);
system(intent);
1
2
3
1
2
3
注:在SDK17之后,由于权限的问题,要记得在加上 –USER 0
例如:AM BROADCAST –USER 0 -A COM.EXAMPLE.TEST
am指令全集
目标文件在\frameworks\base\cmds\am\src\com\android\commands\am
usage: am [subcommand] [options]
usage: am start [-D] [-W] [-P ] [–start-profiler ]
[–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]
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 switch-user
am stop-user
am stack create
am stack movetask [true|false]
am stack resize
am stack boxes
am stack box
am start: start an Activity. Options are:
-D: enable debugging
-W: wait for launch to complete
–start-profiler : start profiler and send results to
-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.
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 switch-user: switch to put USER_ID in the foreground, starting
execution of that user if it is currently stopped.
am stop-user: stop execution of USER_ID, not allowing it to run any
code until a later explicit switch to it.
am stack create: create a new stack relative to an existing one.
: the task to populate the new stack with. Must exist.
: existing stack box’s id.
: 0: before , per RTL/LTR configuration,
1: after , per RTL/LTR configuration,
2: to left of ,
3: to right of , 4: above , 5: below
: float between 0.2 and 0.8 inclusive.
am stack movetask: move from its current stack to the top (true) or bottom (false) of .
am stack resize: change relative size to new .
am stack boxes: list the hierarchy of stack boxes and their contents.
am stack box: list the hierarchy of stack boxes rooted at .
specifications include these flags and arguments:
[-a ] [-d ] [-t ]
[-c [-c ] …]
[-e|–es …]
[–esn …]
[–ez …]
[–ei …]
[–el …]
[–ef …]
[–eu …]
[–ecn ]
[–eia [,