adb(5)-与应用交互

主要是使用 am 命令,常用的 如下:

command用途
start [options] 启动 指定的 Activity
startservice [options] 启动 指定的 Service
broadcast [options] 发送 指定的广播
force-stop 停止 相关的进程

参数很灵活,和写 Android 程序时代码里的 Intent 相对应。

用于决定 intent 对象的选项如下:

参数含义
-a 指定 action,比如 android.intent.action.VIEW
-c 指定 category,比如 android.intent.category.APP_CONTACTS
-n 指定完整 component 名,用于明确指定启动哪个 Activity,如 com.example.app/.ExampleActivity

里还能带数据,就像写代码时的 Bundle 一样:

参数含义
--esn<extra_key> null 值(只有 key 名)
-e|--es<extra_key><extra_string_value> string 值
--ez<extra_key><extra_boolean_value> boolean 值
--ei<extra_key><extra_int_value> integer 值
--el<extra_key><extra_long_value> long 值
--ef<extra_key><extra_float_value> float 值
--eu<extra_key><extra_uri_value> URI
--ecn<extra_key><extra_component_name_value> component name
--eia <extra_key><extra_int_value>[,<extra_int_value...]< code=""> integer 数组
--ela <extra_key><extra_long_value>[,<extra_long_value...]< code=""> long 数组

调起 Activity

命令格式:

adb shell am start [options] 

例如:

adb shell am start -n com.tencent.mm/.ui.LauncherUI

表示调起微信主界面。

adb shell am start -n org.mazhuang.boottimemeasure/.MainActivity --es "toast" "hello, world"

表示调起 org.mazhuang.boottimemeasure/.MainActivity 并传给它 string 数据键值对 toast - hello, world

调起 Service

命令格式:

adb shell am startservice [options] 

例如:

adb shell am startservice -n com.tencent.mm/.plugin.accountsync.model.AccountAuthenticatorService

表示调起微信的某 Service。

发送广播

命令格式:

adb shell am broadcast [options] 

例如:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n org.mazhuang.boottimemeasure/.BootCompletedReceiver

表示向 org.mazhuang.boottimemeasure/.BootCompletedReceiver 发送一个 BOOT_COMPLETED 广播,这类用法在测试的时候很实用,比如某个广播的场景很难制造,可以考虑通过这种方式来发送广播。

强制停止应用

命令:

adb shell am force-stop 

命令示例:

adb shell am force-stop com.qihoo360.mobilesafe

表示停止 360 安全卫士的一切进程与服务。

原文地址:https://www.cnblogs.com/jiablogs/p/8794289.html