0
点赞
收藏
分享

微信扫一扫

ADB 命令之与应用交互相关

RIOChing 2022-12-14 阅读 45


与应用交互

主要是使用 ​​am <command>​​​ 命令,常用的 ​​<command>​​ 如下:

command

用途

​start [options] <INTENT>​

启动 ​​<INTENT>​​ 指定的 Activity

​startservice [options] <INTENT>​

启动 ​​<INTENT>​​ 指定的 Service

​broadcast [options] <INTENT>​

发送 ​​<INTENT>​​ 指定的广播

​force-stop <packagename>​

停止 ​​<packagename>​​ 相关的进程

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

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

参数

含义

​-a <ACTION>​

指定 action,比如 ​​android.intent.action.VIEW​

​-c <CATEGORY>​

指定 category,比如 ​​android.intent.category.APP_CONTACTS​

​-n <COMPONENT>​

指定完整 component 名,用于明确指定启动哪个 Activity,如 ​​com.example.app/.ExampleActivity​

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

参数

含义

​--esn <EXTRA_KEY>​

null 值(只有 key 名)

`-e

--es <EXTRA_KEY> <EXTRA_STRING_VALUE>`

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

integer 数组

​--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]​

long 数组

启动应用/ 调起 Activity

命令格式:

adb shell am start [options] <INTENT>

例如:

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] <INTENT>

例如:

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

表示调起微信的某 Service。

另外一个典型的用例是如果设备上原本应该显示虚拟按键但是没有显示,可以试试这个:

adb shell am startservice -n com.android.systemui/.SystemUIService

停止 Service

命令格式:

adb shell am stopservice [options] <INTENT>

发送广播

命令格式:

adb shell am broadcast [options] <INTENT>

可以向所有组件广播,也可以只向指定组件广播。

例如,向所有组件广播 ​​BOOT_COMPLETED​​:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

又例如,只向 ​​org.mazhuang.boottimemeasure/.BootCompletedReceiver​​​ 广播 ​​BOOT_COMPLETED​​:

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

这类用法在测试的时候很实用,比如某个广播的场景很难制造,可以考虑通过这种方式来发送广播。

既能发送系统预定义的广播,也能发送自定义广播。如下是部分系统预定义广播及正常触发时机:

action

触发时机

android.net.conn.CONNECTIVITY_CHANGE

网络连接发生变化

android.intent.action.SCREEN_ON

屏幕点亮

android.intent.action.SCREEN_OFF

屏幕熄灭

android.intent.action.BATTERY_LOW

电量低,会弹出电量低提示框

android.intent.action.BATTERY_OKAY

电量恢复了

android.intent.action.BOOT_COMPLETED

设备启动完毕

android.intent.action.DEVICE_STORAGE_LOW

存储空间过低

android.intent.action.DEVICE_STORAGE_OK

存储空间恢复

android.intent.action.PACKAGE_ADDED

安装了新的应用

android.net.wifi.STATE_CHANGE

WiFi 连接状态发生变化

android.net.wifi.WIFI_STATE_CHANGED

WiFi 状态变为启用/关闭/正在启动/正在关闭/未知

android.intent.action.BATTERY_CHANGED

电池电量发生变化

android.intent.action.INPUT_METHOD_CHANGED

系统输入法发生变化

android.intent.action.ACTION_POWER_CONNECTED

外部电源连接

android.intent.action.ACTION_POWER_DISCONNECTED

外部电源断开连接

android.intent.action.DREAMING_STARTED

系统开始休眠

android.intent.action.DREAMING_STOPPED

系统停止休眠

android.intent.action.WALLPAPER_CHANGED

壁纸发生变化

android.intent.action.HEADSET_PLUG

插入耳机

android.intent.action.MEDIA_UNMOUNTED

卸载外部介质

android.intent.action.MEDIA_MOUNTED

挂载外部介质

android.os.action.POWER_SAVE_MODE_CHANGED

省电模式开启

(以上广播均可使用 adb 触发)

强制停止应用

命令:

adb shell am force-stop <packagename>

命令示例:

adb shell am force-stop com.qihoo360.mobilesafe

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

收紧内存

命令:

adb shell am send-trim-memory  <pid> <level>

pid: 进程 ID level: HIDDEN、RUNNING_MODERATE、BACKGROUND、 RUNNING_LOW、MODERATE、RUNNING_CRITICAL、COMPLETE

命令示例:

adb shell am send-trim-memory 12345 RUNNING_LOW

表示向 pid=12345 的进程,发出 level=RUNNING_LOW 的收紧内存命令。

举报

相关推荐

0 条评论