Android 开发中最常用的adb命令行

10 阅读3分钟

1.查看设备连接状态

// 查看连接的设备
adb devices

// 获取设备序列号
adb get-serialno

// 多设备连接时,使用-s指定设备
adb -s <设备序列号> install <apk路径>

// 覆盖安装(保留数据)
adb -s <设备序列号> install -r <apk路径>

// 降级安装(允许安装更低版本)
adb -s <设备序列号> install -d <apk路径>

2.深浅模式切换

// 切换深色模式
adb shell cmd uimode night yes
Night mode: yes

// 切换浅色模式
adb shell cmd uimode night no
Night mode: no

3.查看app安装路径

adb shell pm path packageName

4.查看 Android 设备上无线网卡 (wlan0) 的配置信息

// 详细信息
adb shell ifconfig wlan0
// ip信息
adb shell ip -4 addr show wlan0

5.切换无线调试

// 设置端口:如5555
adb tcpip 5555

// 无线连接
adb connect yourIp:5555

6.启动Activity

场景关键参数说明命令示例
按包名/类名启动 (最常用)-n:指定 Component (组件)adb shell am start -n com.example.app/.MainActivity
隐式 Intent 启动-a: Action (动作)
-d: Data URI (数据)
-t: MIME Type (类型)
-c: Category (类别)
adb shell am start -a android.intent.action.VIEW
-d "developer.android.com"
-t "text/html"
-c android.intent.category.BROWSABLE
-c android.intent.category.DEFAULT
携带额外数据 (Extra) 启动-es: 字符串
--ez: 布尔值
--ei: 整型
--el: 长整型
--ef: 浮点型
adb shell am start -n com.example.app/.TestActivity --ez is_test true --es name "John"
启动带 Flags 的 Activity-f: Intent Flags (标记)adb shell am start -n com.example.app/.MainActivity -f 0x10000000
等待 Activity 返回结果-W: 以同步方式等待启动完成adb shell am start -W -n com.example.app/.MainActivity
以调试模式启动-D: 启动并等待调试器 (Debugger) 连接adb shell am start -D -n com.example.app/.MainActivity
启动并模拟冷启动-S: 在启动前强制终止目标应用adb shell am start -S -n com.example.app/.MainActivity
重复启动-R <次数>: 重复启动 Activity
-S: 通常在重复启动时使用
adb shell am start -R 10 -n com.example.app/.MainActivity

7.发送广播

场景关键参数/选项命令示例
不带参数的自定义广播-a adb shell am broadcast -a com.example.REFRESH_UI
带字符串参数--es adb shell am broadcast -a com.example.USER_ACTION --es name "John"
带整型参数--ei adb shell am broadcast -a com.example.USER_ACTION --ei age 25
带布尔参数--ez <true/false>adb shell am broadcast -a com.example.USER_ACTION --ez isVip true
带浮点参数--ef adb shell am broadcast -a com.example.USER_ACTION --ef price 19.99
带长整型参数--el adb shell am broadcast -a com.example.USER_ACTION --el timestamp 123456789
混合多个参数组合使用上述参数adb shell am broadcast -a com.example.USER_ACTION --es name "John" --ei age 25 --ez isVip true
指定包名(Android 8.0+ 推--package <package_name>adb shell am broadcast -a com.example.MY_ACTION --package com.example.targetapp
指定接收器类名-n <package/class>adb shell am broadcast -a com.example.MY_ACTION -n com.example.targetapp/.MyReceiver
带自定义权限发送--receiver-permission adb shell am broadcast -a com.example.PROTECTED_ACTION --receiver-permission com.example.MY_PERMISSION

8.启动服务

场景关键参数/选项命令示例
按组件名启动(最常用)-n 指定服务全类名adb shell am startservice -n com.example.app/.MyService
隐式 Intent 启动-a Action 或 -p 包名adb shell am startservice -a com.example.START_MY_SERVICE
携带字符串参数--es adb shell am startservice -n com.example.app/.MyService --es data "hello"
携带整型参数--ei adb shell am startservice -n com.example.app/.MyService --ei id 123
携带布尔参数--ez <true/false>adb shell am startservice -n com.example.app/.MyService --ez enable true
携带浮点参数--ef adb shell am startservice -n com.example.app/.MyService --ef ratio 0.75
混合多个参数 --es/--ei/--ez 组合使用多个adb shell am startservice -n com.example.app/.MyService --es name "John" --ei age 25
启动前台服务(Android 8.0+) start-foreground-service 替代 startservice使用adb shell am start-foreground-service -n com.example.app/.MyForegroundService
等待服务启动完成(同步)-W 参数adb shell am startservice -W -n com.example.app/.MyService
指定创建服务的用户--user <user_id>adb shell am startservice --user 0 -n com.example.app/.MyService
多次尝试启动--repeat adb shell am startservice --repeat 3 -n com.example.app/.MyService
带调试模式启动--debugadb shell am startservice --debug -n com.example.app/.MyService