adb shell am 命令

本文介绍了如何使用adb shell am命令来启动activity、服务、发送广播等操作,包括命令的基本用法、参数说明及实例演示。


adb shell am
使用此命令可以从cmd控制台启动 activity, services;发送 broadcast等等:
 
C:\Users\Administrator>adb shell am
usage: am [subcommand] [options]

    start an Activity: am start [-D] [-W] <INTENT>
        -D: enable debugging
        -W: wait for launch to complete

    start a Service: am startservice <INTENT>

    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

    start monitoring: am monitor [--gdb <port>]
        --gdb: start gdbserv on the given port at crash/ANR

    <INTENT> specifications include these flags:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--esn <EXTRA_KEY> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [-n <COMPONENT>] [-f <FLAGS>]
        [--grant-read-uri-permission] [--grant-write-uri-permission]
        [--debug-log-resolution]
        [--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]
        [--receiver-registered-only] [--receiver-replace-pending]
        [<URI>]


使用实例:

如启动一个 Activity:

 

格式:

adb shell am start -n 包名/包名+类名(-n 类名,-a action,-d date,-m MIME-TYPE,-c category,-e 扩展数据,等)。

 

实例1: 

C:\Users\Administrator>adb shell am start -n com.android.camera/.Camera
Starting: Intent { cmp=com.android.camera/.Camera }

 

实例2:(带extra 的 intent)

C:\Users\Administrator>adb shell am start -n com.android.camera/.Camera -e abc hello
Starting: Intent { cmp=com.android.camera/.Camera (has extras) }

其中 extra 的 key 为 abc ,value 为字串 "hello"

 

 

 

 

adb shell am instrument [options] <COMPONENT>

作用:启动对instrument实例的监视。

参数[options]:

-e <key> <value> // -e选项需要放在-w选项之前

作用:提供了以键值对形式存在的测试选项。Android中提供了多种键值对,具体参见下表。
举例:-e class com.android.phone.FIncomingCallTests#testRejectCall

-r

作用:以原始形式输出测试结果。该选项通常是在性能测试时与-e perf true一起使用。

参数 <COMPONENT>:

-w <test_package_name>/<runner_class> //<test_package_name>和<runner_class>在测试工程的AndroidManifest.xml中查找

作用:保持adb shell打开直至测试完成
举例:-w com.android.phone.tests/com.android.phone.runners.FunctionalTestRunner

<key> <value>参考表

Key Value Description
package <Java_package_name> The fully-qualified Java package name for one of the packages in the test application. Any test case class that uses this package name is executed. Notice that this is not an Androidpackage name; a test package has a single Android package name but may have several Java packages within it.
class <class_name> The fully-qualified Java class name for one of the test case classes. Only this test case class is executed.
<class_name>#method name A fully-qualified test case class name, and one of its methods. Only this method is executed. Note the hash mark (#) between the class name and the method name.
func true Runs all test classes that extendInstrumentationTestCase.
unit true Runs all test classes that do not extend eitherInstrumentationTestCase orPerformanceTestCase.
size [small | medium | large] Runs a test method annotated by size. The annotations are @SmallTest@MediumTest, and@LargeTest.
perf true Runs all test classes that implementPerformanceTestCase. When you use this option, also specify the -r flag for am instrument, so that the output is kept in raw format and not re-formatted as test results.
debug true Runs tests in debug mode.
log true Loads and logs all specified tests, but does not run them. The test information appears inSTDOUT. Use this to verify combinations of other filters and test specifications.
emma true Runs an EMMA code coverage analysis and writes the output to /data//coverage.ec on the device. To override the file location, use thecoverageFile key that is described in the following entry.

Note: This option requires an EMMA-instrumented build of the test application, which you can generate with the coverage target.

coverageFile <filename>

Overrides the default location of the EMMA coverage file on the device. Specify this value as a path and filename in UNIX format. The default filename is described in the entry for theemma key

 

adb shell am start [options] <INTENT>

作用:启动一个activity

举例:adb shell am start -a com.lt.test.action.SECOND

举例:adb shell am start -n com.lt.test/.MyActivity

说明:[options]与<INTENT>参见 http://developer.android.com/tools/help/adb.html#am

adb shell am startservice [options] <INTENT>

作用:启动一个service

举例:adb shell am startservice -a com.lt.test.action.ONESERVICE
举例:adb shell am startservice -n com.lt.test/.MyService

 

adb shell am force-stop <PACKAGE>
作用:强制关闭一个应用程序

举例:adb shell am force-stop com.lt.test

 

adb shell am broadcast [options] <INTENT>

作用:发送一个广播
举例:adb shell am broadcast -a "action_finish" (发送一个广播去关闭一个activity)
举例:adb shell am broadcast -a android.intent.action.MASTER_CLEAR(恢复出厂设置的方法,会清除内存所有内容)

举例:adb shell am broadcast -n com.lt.test/.MyBroadcast

<think>我们被要求使用adb shell am start命令,特别是带-w选项来启动一个Android应用组件并等待其完成。 根据引用[1]:adb启动程序命令adb shell am start。但没有详细说明-w选项。 我们需要更详细的信息:-w选项用于等待启动完成,并打印启动时间。 实际上,am start命令的完整用法如下(通过adb shell am start -h可以查看): start [options] <INTENT> options包括: -D: 启用调试 -W: 等待启动完成 --start-profiler <FILE>: 启动分析器并将结果发送到<FILE> -P <FILE>: 类似--start-profiler,但分析器在应用进入空闲状态时停止 -R <COUNT>: 重复执行活动启动<COUNT>次 -S: 在启动Activity前强行停止目标应用 --opengl-trace: 启用OpenGL函数的跟踪 --user <USER_ID> | current: 指定要作为哪个用户运行;如果未指定,则作为当前用户运行 因此,-W选项就是用来等待启动完成的。 示例:启动包名为com.example.app的应用的主Activity,并等待其完成: adb shell am start -W com.example.app/.MainActivity 输出会包含: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.app/.MainActivity } Status: ok Activity: com.example.app/.MainActivity ThisTime: 100 TotalTime: 100 WaitTime: 150 Complete 其中,ThisTime: 最后一个Activity启动耗时; TotalTime: 所有Activity启动耗时; WaitTime: AMS启动Activity的总耗时。 注意:Intent可以通过组件名指定,也可以通过Action和Category等指定。 例如,通过Action和Category启动: adb shell am start -W -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n com.example.app/.MainActivity 但是,如果应用已经启动,可能不会等待到界面完全绘制(因为Activity已经存在,只是被带到前台)。为了确保启动新的实例,可以添加标志FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TOP等,但通常am start会默认添加FLAG_ACTIVITY_NEW_TASK。 另外,如果应用尚未安装或组件不存在,命令会报错。 因此,针对用户的问题,我们给出使用-W选项的示例,并解释输出。 注意:如果启动的Activity是应用的主Activity,通常我们使用包名/主Activity的完整类名(如com.example.app/.MainActivity),其中MainActivity前面的点表示包名后面的类名。 如果不知道主Activity,可以通过以下方式获取: adb shell dumpsys package <package_name> | grep -A 1 "android.intent.action.MAIN" 但这不是必须的,因为通常开发者知道主Activity。 另外,引用[3]中提到了input命令,但这里不相关。 根据引用[2]中dumpsys的用法,可以用于获取网络状态等信息,但和启动应用无关。 因此,我们聚焦在am start -w的使用。 下面我们给出具体步骤和示例。</think>### 启动Android应用组件并等待完成的adb命令详解 `adb shell am start -w`命令用于启动Android应用组件并等待其完全启动完成。其中`-w`参数是关键,它会强制命令等待直到Activity完全启动并显示在屏幕上才返回结果[^1]。 #### 命令基本格式 ```bash adb shell am start -W [选项] <组件名> ``` #### 核心参数说明 1. **`-W`(必选)** 等待启动完成,并输出启动耗时统计: ```bash adb shell am start -W com.example.app/.MainActivity ``` 输出示例: ``` Status: ok Activity: com.example.app/.MainActivity ThisTime: 250 # 当前Activity启动耗时 TotalTime: 400 # 包括依赖资源的整体耗时 WaitTime: 600 # AMS(Activity Manager Service)总处理时间 ``` 2. **组件名格式** `<包名>/<完整Activity类名>` 例如:`com.tencent.mm/.ui.LauncherUI` 3. **常用附加选项** - `-n`:显式指定组件名 ```bash adb shell am start -W -n com.example.app/.MainActivity ``` - `-a`:指定Action(如主屏启动) ```bash adb shell am start -W -a android.intent.action.MAIN ``` - `-c`:添加Category ```bash adb shell am start -W -c android.intent.category.LAUNCHER ``` - `-f`:设置Intent Flags ```bash adb shell am start -W -f 0x10200000 ``` #### 典型使用场景 1. **冷启动性能测试** ```bash # 先强制停止应用 adb shell am force-stop com.example.app # 冷启动并测量时间 adb shell am start -W -n com.example.app/.MainActivity ``` 2. **带参数启动Activity** ```bash adb shell am start -W -a android.intent.action.VIEW \ -d "https://example.com" com.example.app/.WebViewActivity ``` 3. **调试模式启动** ```bash adb shell am start -W -D -n com.example.app/.DebugActivity ``` #### 注意事项 1. 确保设备已开启USB调试(`adb devices`验证连接) 2. 组件名必须完全匹配(可通过`adb shell dumpsys package <包名>`查询) 3. 当出现`Error: Activity not started`时,检查: - 组件名是否拼写错误 - Activity是否在AndroidManifest.xml中导出 - 是否缺少必要权限[^3] > 通过`-w`参数获取的启动时间(`TotalTime`)是性能优化的重要指标,常用于自动化测试脚本中[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值