> 文档中心 > Android开发-使用am命令启动Activity,Service等组件,以及发送广播等操作

Android开发-使用am命令启动Activity,Service等组件,以及发送广播等操作


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 ] [--start-profiler ]

          [--sampling INTERVAL] [--streaming] [-R COUNT] [-S]

          [--track-allocation] [--user | current]

      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)

      --streaming: stream the profiling output to the specified file

          (use with --start-profiler)

      -P : like above, but profiling stops when app goes idle

      --attach-agent : attach the given agent before binding

      -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.

  start-service [--user | current]

      Start a Service.  Options are:

      --user | current: Specify which user to run as; if not

          specified then run as the current user.

  start-foreground-service [--user | current]

am命令常用参数说明:

命令 功能 实现方法
am start [options] 启动Activity startActivityAsUser
am startservice 启动Service startService
am stopservice 停止Service stopService
am broadcast 发送广播 broadcastIntent
am restart 重启 restart
am dumpheap 进程pid的堆信息输出到file dumpheap
am send-trim-memory 收紧进程的内存 setProcessMemoryTrimLevel
am kill 杀指定后台进程 killBackgroundProcesses
am kill-all 杀所有后台进程 killAllBackgroundProcesses
am force-stop 强杀进程 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 ]
 [-d ]
 [-t ]
 [-c [-c ] ...]
 [-e|--es ...]
 [--ez ...]
 [-e|--ei ...]
 [-n ]
 [-f ] []