• Android am 指令的使用



    http://blog.csdn.net/stoppig/article/details/50715773


    何为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
    • 还可放在代码中操作。
    <code class="hljs sql has-numbering">    char intent[1024] = {0};
        sprintf(intent, "am <span class="hljs-operator"><span class="hljs-keyword">start</span> -a com.android.browser.ProxySelector);</span>
        system(intent);</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>

    注:在SDK17之后,由于权限的问题,要记得在加上 –USER 0
    例如:AM BROADCAST –USER 0 -A COM.EXAMPLE.TEST

    am指令全集

    目标文件在frameworksasecmdsamsrccomandroidcommandsam

    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 [,


  • 相关阅读:
    在OpenEuler中安装轻量化调试工具CGDB
    nginx服务器 server location映射路径配置
    hadoop HDFS文件系统角色及文件读写流程
    Linux系统Tomcat服务自启动脚本
    Linux系统软件安装的三种方式
    linux基础命令
    Java多线程解析
    多线程的典型应用场景---多个生产者多个消费者对共享资源的处理
    10.16变量的作用域和生存周期
    10.15sizeof用法
  • 原文地址:https://www.cnblogs.com/ztguang/p/12646122.html
Copyright © 2020-2023  润新知