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 <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]启动ActivitystartActivityAsUser
am startservice启动ServicestartService
am stopservice停止ServicestopService
am broadcast发送广播broadcastIntent
am restart重启restart
am dumpheap <pid> <file>进程pid的堆信息输出到filedumpheap
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>]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值