[5] ADB 与应用交互
2023-09-11 14:19:17 时间
主要是使用 am <command>
命令,常用的 <command>
如下:
command | 用途 |
---|---|
start [options] <INTENT> | 启动 指定的 Activity |
startservice [options] <INTENT> | 启动 指定的 Service |
broadcast [options] <INTENT> | 发送 指定的广播 |
force-stop <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 |
1. 启动应用/ 调起 Activity
adb shell am start [options] <INTENT>
栗子:调起微信的主界面
adb shell am start -n com.tencent.mm/.ui.LauncherUI
2. 调起 Service
adb shell am startservice [options] <INTENT>
3. 停止 Service
adb shell am stopservice [options] <INTENT>
4. 发送广播
adb shell am broadcast [options] <INTENT>
5. 强制停止应用
adb shell am force-stop <packagename>
6. 收紧内存
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 的收紧内存命令。