常用 adb shell 命令大全

| 选择喜欢的代码风格  

adb 基本命令 - 电脑上操作的命令


#查看设备
adb devices #查看当前连接的设备

#利用无线来查看 adb shell
> adb tcpip 5555

连接:
> adb connect IP:5555

adb  install <apk文件路径> #安装软件:将指定的apk文件安装到设备上
#如: adb install com.command.not.found
#参数“-r”,它是更新安装的意思,参数 -s ,安装到sdcard.

adb  uninstall <包名> #卸载软件,
#如果加 -k 参数,为卸载软件但是保留配置和缓存文件.

#登录设备 shell
adb shell <command命令> 
#如:adb  shell cat /proc/cpuinfo 

#从电脑上发送文件到设备 :可以把本机电脑上的文件或者文件夹复制到设备(手机)
adb push <本地路径> <远程路径>

#从设备上下载文件到电脑:可以把设备(手机)上的文件或者文件夹复制到本机电脑
adb pull <远程路径> <本地路径>

#同步更新
adb sync [ <directory> ]
#如: adb sync /data/ 如果不指定目录,将同时更新 /data 和 /system/

adb help #显示帮助信息
adb  remount #重新挂载

#启动和关闭server
adb start-server             - ensure that there is a server running
adb kill-server              - kill the server if it is running

#重启设备
adb reboot [bootloader|recovery]

#关机
adb shell
su
reboot -p

#查看Log
[adb] logcat [<option>] ... [<filter-spec>] ..
-b  加载一个可使用的日志缓冲区供查看,比如event 和radio . 默认值是main 。具体查看Viewing   Alternative Log Buffers.
-c 清楚屏幕上的日志.
-d 输出日志到屏幕上.
-f  指定输出日志信息的 ,默认是stdout .
-g 输出指定的日志缓冲区,输出后退出.
-n  设置日志的最大数目 .,默认值是4,需要和 -r 选项一起使用。
-r  每 时输出日志,默认值为16,需要和-f 选项一起使用.
-s 设置默认的过滤级别为silent.
-v  设置日志输入格式,默认的是brief 格式

#查看 bug 报告
adb bugreport

adb shell 命令 - 手机设备上操作的命令


#按键事件
input text <string>   input a string to device
input keyevent <event_code>   send a Key Event to device

#keyevent
adb shell input keyevent 20 #向下
adb shell input keyevent 4 #返回
adb shell input keyevent 3 #Home
adb shell input keyevent 6 #挂机
adb shell input keyevent 84 #搜索
adb shell input keyevent 26 #电源,息屏
adb shell input keyevent 24 #音量+
adb shell input keyevent 25 #音量-

#导航
3 Home
4 Back
19 Up
20 Down
21 Left
22 Right
23 Select/Ok
24 Volume+
25 Volume-
82 Menu 菜单

adb shell input text "ANDROID" 支持的KEYCODE
0 -->  "KEYCODE_UNKNOWN"
1 -->  "KEYCODE_MENU"
2 -->  "KEYCODE_SOFT_RIGHT"
3 -->  "KEYCODE_HOME"
4 -->  "KEYCODE_BACK"
5 -->  "KEYCODE_CALL" 
6 -->  "KEYCODE_ENDCALL" 
7 -->  "KEYCODE_0" 
8 -->  "KEYCODE_1" 
9 -->  "KEYCODE_2" 
10 -->  "KEYCODE_3"
11 -->  "KEYCODE_4" 
12 -->  "KEYCODE_5" 
13 -->  "KEYCODE_6" 
14 -->  "KEYCODE_7" 
15 -->  "KEYCODE_8" 
16 -->  "KEYCODE_9" 
17 -->  "KEYCODE_STAR" 
18 -->  "KEYCODE_POUND" 
19 -->  "KEYCODE_DPAD_UP" 
20 -->  "KEYCODE_DPAD_DOWN" 
21 -->  "KEYCODE_DPAD_LEFT" 
22 -->  "KEYCODE_DPAD_RIGHT"
23 -->  "KEYCODE_DPAD_CENTER"
24 -->  "KEYCODE_VOLUME_UP" 
25 -->  "KEYCODE_VOLUME_DOWN" 
26 -->  "KEYCODE_POWER" 
27 -->  "KEYCODE_CAMERA" 
28 -->  "KEYCODE_CLEAR" 
29 -->  "KEYCODE_A" 
30 -->  "KEYCODE_B" 
31 -->  "KEYCODE_C" 
32 -->  "KEYCODE_D" 
33 -->  "KEYCODE_E" 
34 -->  "KEYCODE_F" 
35 -->  "KEYCODE_G" 
36 -->  "KEYCODE_H" 
37 -->  "KEYCODE_I" 
38 -->  "KEYCODE_J" 
39 -->  "KEYCODE_K" 
40 -->  "KEYCODE_L" 
41 -->  "KEYCODE_M"
42 -->  "KEYCODE_N" 
43 -->  "KEYCODE_O" 
44 -->  "KEYCODE_P" 
45 -->  "KEYCODE_Q" 
46 -->  "KEYCODE_R" 
47 -->  "KEYCODE_S" 
48 -->  "KEYCODE_T" 
49 -->  "KEYCODE_U" 
50 -->  "KEYCODE_V" 
51 -->  "KEYCODE_W" 
52 -->  "KEYCODE_X"
53 -->  "KEYCODE_Y" 
54 -->  "KEYCODE_Z" 
55 -->  "KEYCODE_COMMA" 
56 -->  "KEYCODE_PERIOD"
57 -->  "KEYCODE_ALT_LEFT" 
58 -->  "KEYCODE_ALT_RIGHT" 
59 -->  "KEYCODE_SHIFT_LEFT" 
60 -->  "KEYCODE_SHIFT_RIGHT" 
61 -->  "KEYCODE_TAB" 
62 -->  "KEYCODE_SPACE" 
63 -->  "KEYCODE_SYM" 
64 -->  "KEYCODE_EXPLORER" 
65 -->  "KEYCODE_ENVELOPE" 
66 -->  "KEYCODE_ENTER" 
67 -->  "KEYCODE_DEL" 
68 -->  "KEYCODE_GRAVE" 
69 -->  "KEYCODE_MINUS" 
70 -->  "KEYCODE_EQUALS" 
71 -->  "KEYCODE_LEFT_BRACKET" 
72 -->  "KEYCODE_RIGHT_BRACKET" 
73 -->  "KEYCODE_BACKSLASH"
74 -->  "KEYCODE_SEMICOLON" 
75 -->  "KEYCODE_APOSTROPHE"
76 -->  "KEYCODE_SLASH" 
77 -->  "KEYCODE_AT" 
78 -->  "KEYCODE_NUM" 
79 -->  "KEYCODE_HEADSETHOOK" 
80 -->  "KEYCODE_FOCUS"
81 -->  "KEYCODE_PLUS"
82 -->  "KEYCODE_MENU"
83 -->  "KEYCODE_NOTIFICATION"
84 -->  "KEYCODE_SEARCH" 
85 -->  "TAG_LAST_KEYCODE"  

-----------------------------------
#am 命令
am start [options] #启动Activity
am start -n com.eg.android.AlipayGphone/com.alipay.mobile.payee.ui.PayeeQRSetMoneyActivity
am startservice #启动Service
am startservice com.miui.powerkeeper
am stopservice  #停止Service
am broadcast #发送广播
am kill #kill指定进程
am force-stop #强制停止某进程
am hang #系统hang住
am restart #重启手机
am dumpheap #将进程pid的堆信息输出到file

-----------------------------------
#pm 命令
pm list packages #列举app包信息
pm install [options] #安装应用
pm uninstall [options] #卸载应用
pm hide #隐藏应用
pm unhide #显示应用
pm get-install-location #获取安装位置
pm clear #清空App数据
pm force-dex-opt #dex优化
pm dump #dump 信息

-----------------------------------
#dumpsys 命令
dumpsys activit
dumpsys activity intents
dumpsys activity broadcasts
dumpsys activity providers
dumpsys activity services
dumpsys activity activities
dumpsys activity processes

#获取当前activity信息
adb shell dumpsys activity top 

dumpsys window
dumpsys window windows
dumpsys window tokens
dumpsys window sessions
dumpsys window policy
dumpsys window input

#获取当前 activity 名称
adb shell dumpsys window windows | findstr Current

adb shell 其他命令


#模拟点击
adb shell input tap 100 100

#滑动
adb shell input swipe x1 y1 x2 y2 
adb input touchscreen swipe x1 y1 x2 y2 100

adb shell input swipe 100 100 400 100  300 #左往右
adb shell input swipe 400 100 100 100  300 #右往左
adb shell input swipe 100 100 100 400  300 #上往下
adb shell input swipe 100 400 100 100  300 #下往上
adb shell input swipe 100 100 400 400  300 #上往下斜
adb shell input swipe 400 400 100 100  300 #下往上斜

#长按
adb shell input swipe 100 100 100 100  1000 //在 100 100 位置长按 1000毫秒

adb shell input swipe 367 469 367 469 800

#打印所有包名
adb shell pm list packages

➜  ~ adb shell pm list packages
package:com.huawei.floatMms
package:com.android.defcontainer
package:com.tencent.mm
...

#打印制定包的apk路径
adb shell pm path com.android.phone

➜  ~ adb shell pm path com.huawei.android.launcher
package:/system/app/HwLauncher6.apk

#截图
adb shell screencap /sdcard/screen.png
adb pull /sdcard/screen.png #下载到本地

#获取被点击的位置信息
adb shell getevent

>
/dev/input/event0 3 39 3e1
/dev/input/event0 1 14a 1
/dev/input/event0 1 145 1
/dev/input/event0 3 35 406  //x坐标
/dev/input/event0 3 54 1083  //y坐标
/dev/input/event0 0 0 0
/dev/input/event0 3 39 ffffffff
/dev/input/event0 1 14a 0
/dev/input/event0 1 145 0
/dev/input/event0 0 0

getevent -l -c 16
输出所有event设备的基本信息

add device 1: /dev/input/event2
  name:     "hi6421_on"
could not get driver version for /dev/input/mouse0, Not a typewriter
add device 2: /dev/input/event4
  name:     "huawei,touchscreen"
add device 3: /dev/input/event0
  name:     "mhl_rcp_dev"
could not get driver version for /dev/input/mice, Not a typewriter
add device 4: /dev/input/event1
  name:     "hisi_gpio_key.14"
add device 5: /dev/input/event3
  name:     "hi3630_hi6401_CARD Headset Jack"

getevent -c 10 //输出10条信息后退出
getevent -l  //将type、code、value以对应的常量名称显示

#使用 uiautomator dump 获取app上的页面元素
adb shell uiautomator dump /data/local/tmp/uidump.xml
adb shell uiautomator dump /sdcard/dump.xml

ADB 命令扩展阅读:




发表评论