当前位置: 首页 > 工具软件 > Amandroid > 使用案例 >

android am 命令总结

牧甫
2023-12-01
文章出处: http://blog.csdn.net/shift_wwx
Android在shell中提供了 am 命令来发送Intent,它的源码为位于 frameworks/base/cmds/am.
它本身是用JAVA代码来实现的。
执行 am 命令其实是通过运行shell脚本 frameworks/base/cmds/am/am,然后在该脚本中运行 app_process 命令来启动am这个java程序的。
frameworks/base/cmds/am/am脚本文件如下:

# Script to start "am" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am "$@"

am命令语法如下
usage: am [subcommand] [options]
 
CommandDescription
start [options] <INTENT>Start an Activity specified by<INTENT>.

See the Specification for <INTENT> arguments.

Options are:

  • -D: Enable debugging.
  • -W: Wait for launch to complete.
  • --start-profiler <FILE>: Start profiler and send results to <FILE>.
  • -P <FILE>: Like --start-profiler, but profiling stops when the app goes idle.
  • -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.
  • --opengl-trace: Enable tracing of OpenGL functions.
  • --user <USER_ID> | current: Specify which user to run as; if not specified, then run as the current user.

startservice [options] <INTENT>Start the Service specified by<INTENT>.

See the Specification for <INTENT> arguments.

Options are:

  • --user <USER_ID> | current: Specify which user to run as; if not specified, then run as the current user.

force-stop <PACKAGE>Force stop everything associated with <PACKAGE> (the app's package name).
kill [options] <PACKAGE>Kill all processes associated with <PACKAGE> (the app's package name). This command kills only processes that are safe to kill and that will not impact the user experience.

Options are:

  • --user <USER_ID> | all | current: Specify user whose processes to kill; all users if not specified.

kill-all

Kill all background processes.

broadcast [options] <INTENT> Issue a broadcast intent.

See the Specification for <INTENT> arguments.

Options are:

  • [--user <USER_ID> | all | current]: Specify which user to send to; if not specified then send to all users.
instrument [options] <COMPONENT>Start monitoring with an Instrumentation instance. Typically the target<COMPONENT> is the form<TEST_PACKAGE>/<RUNNER_CLASS>.

Options are:

  • -r: Print raw results (otherwise decode <REPORT_KEY_STREAMRESULT>). Use with[-e perf true] to generate raw output for performance measurements.
  • -e <NAME> <VALUE>: Set argument <NAME> to <VALUE>. For test runners a common form is-e <testrunner_flag> <value>[,<value>...].
  • -p <FILE>: Write profiling data to <FILE>.
  • -w: Wait for instrumentation to finish before returning. Required for test runners.
  • --no-window-animation: Turn off window animations while running.
  • --user <USER_ID> | current: Specify which user instrumentation runs in; current user if not specified.
profile start <PROCESS> <FILE>Start profiler on <PROCESS>, write results to <FILE>.
profile stop <PROCESS>Stop profiler on <PROCESS>.
dumpheap [options] <PROCESS> <FILE>Dump the heap of <PROCESS>, write to <FILE>.

Options are:

  • --user [<USER_ID>|current]: When supplying a process name, specify user of process to dump; uses current user if not specified.
  • -n: Dump native heap instead of managed heap.
set-debug-app [options] <PACKAGE>Set application <PACKAGE> to debug.

Options are:

  • -w: Wait for debugger when application starts.
  • --persistent: Retain this value.
clear-debug-app Clear the package previous set for debugging with set-debug-app.
monitor [options] Start monitoring for crashes or ANRs.

Options are:

  • --gdb: Start gdbserv on the given port at crash/ANR.

screen-compat [on|off] <PACKAGE> Control screen compatibility mode of <PACKAGE>.
display-size [reset|<WxH>] Override emulator/device display size. This command is helpful for testing your app across different screen sizes by mimicking a small screen resolution using a device with a large screen, and vice versa.

Example:
am display-size 1280x800

display-density <dpi> Override emulator/device display density. This command is helpful for testing your app across different screen densities on high-density screen environment using a low density screen, and vice versa.

Example:
am display-density 480

to-uri <INTENT> Print the given intent specification as a URI.

See the Specification for <INTENT> arguments.

to-intent-uri <INTENT> Print the given intent specification as an intent: URI.

See the Specification for <INTENT> arguments.


 
<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

# am start -n 包名/包名.活动(activity)名称
   启动的哪个acitivity方法可以从每个应用的AndroidManifest.xml的文件中找到信息
 

启动照相机:

# am start -n com.android.camera/com.android.camera.Camera

启动浏览器

# 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

 

profile

#am profile 进程号 start profile_result.txt

#am profile 进程号 stop

 

启动一个service

#am startservice service的intent

 

启动instrument测试(界面上是进dev tools -->instrument选择)

看看浏览器测试工程的xml文件

<application>
        <uses-library android:name="android.test.runner" />
</application>

    <!--
    This declares that this app uses the instrumentation test runner targeting
    the package of com.android.email.  To run the tests use the command:
    "adb shell am instrument -w com.android.browser.tests/android.test.InstrumentationTestRunner"
    -->
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.android.browser"
                     android:label="Tests for Browser."/>

    <instrumentation android:name="com.android.browser.BrowserLaunchPerformance"
        android:targetPackage="com.android.browser"
        android:label="Browser Launch Performance">
    </instrumentation>

 

 

将当前浏览器加到单元测试中

# am instrument -w com.android.Browser/android.test.InstrumentationTestRunner

运行某个TestCase:
# am instrument -w -e class com.android.BrowserTest.PopularUrlsTest com.android.Browser/android.test.InstrumentationTestRunner

运行一个TestCase中的某个功能:
adb shell am instrument -w -e class com.android.BrowserTest.PopularUrlsTest#testStability com.android.Browser/android.test.InstrumentationTestRunner

同时测试多个TestCase:
#am instrument -w -e class com.android.BrowserTest.PopularUrlsTest,TestWebViewClient.java com.android.Browser/android.test.InstrumentationTestRunner

 

public class ApiDemosRunner extends InstrumentationTestRunner{
    @Override
    public TestSuite getAllTests(){
        Log.i(”ApiDemosRunner”, “ApiDemosRunner::getAllTests()”);
        return new TestSuiteBuilder(ApiDemosRunner.class).includeAllPackagesUnderHere().build();   
    }
    
    @Override
    public ClassLoader getLoader(){
        return ApiDemosRunner.class.getClassLoader();
    }
}
 类似资料: