adb命令
Whisper Lv4

adb命令

adb的全称为Android Debug Bridge,就是起到调试桥的作用。通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具。adb的工作方式比较特殊,采用监听Socket TCP 5554等端口的方式让IDE和Qemu通讯,默认情况下adb会daemon相关的网络端口,所以当我们运行Eclipse时adb进程就会自动运行。

借助adb工具,我们可以管理设备或手机模拟器的状态。还可以进行很多手机操作,如安装软件、系统升级、运行shell命令等等。其实简而言说,adb就是连接Android手机与PC端的桥梁,可以让用户在电脑上对手机进行全面的操作。

ADB是一个C/S架构的应用程序,由三部分组成:
1)adb client, 运行在PC上(为DDMS,即IDE工作)
2)adb daemon(守护进程),运行于Emulator(为与Emulator中的VM交互工作);
3)adb server(服务进程),运行在PC(任务管理器上有),管理着adb client和adb daemon的通信。

查看设备

adb devices

这个命令是查看当前连接的设备, 连接到计算机的android设备或者模拟器将会列出显示。

安装软件

adb install

卸载软件

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

进入设备或模拟器的shell:

adb shell

通过上面的命令,就可以进入设备或模拟器的shell环境中,在这个Linux Shell中,你可以执行各种Linux的命令,另外如果只想执行一条shell命令,可以采用以下的方式:

adb shell [command]

如:adb shell dmesg会打印出内核的调试信息。

发布端口

可以设置任意的端口号,做为主机向模拟器或设备的请求端口。如:

1
adb forward tcp:5555 tcp:8000

从电脑上发送文件到设备

1
adb push <本地路径> <远程路径>

用push命令可以把本机电脑上的文件或者文件夹复制到设备(手机)

从设备上下载文件到电脑

1
adb pull <远程路径> <本地路径>

用pull命令可以把设备(手机)上的文件或者文件夹复制到本机电脑

查看bug报告

1
adb bugreport

记录无线通讯日志

一般来说,无线通讯的日志非常多,在运行时没必要去记录,但我们还是可以通过命令,设置记录:

1
2
3
adb shell

logcat -b radio

获取设备的ID和序列号

1
2
3
4
5
6
7
adb get-product

adb get-serialno

adb shell

sqlite3

写入日志

1
adb devicesadb logcat -v time >log.txt

查看Android的activity

手机连接PC启动要查看的应用,或者启动模拟器然后启动要查看的应用,cmd输入命令:

1
2
adb shell dumpsys activity | findstr "mFocusedActivity"

输出:

1
mFocusedActivity: ActivityRecord{f0d11de u0 com.android.calculator2/.Calculator t30}

activity名称为:.Calculator

adb截图功能

查看adb截图帮助命令

1
2
3
4
5
6
7
> adb shell screenrecord -h
usage: screencap [-hp] [-d display-id] [FILENAME]
-h: this message
-p: save the file as a png.
-d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

使用

1
2
3

adb shell /system/bin/screencap -p /sdcard/screenshot.png(保存到SDCard)
adb pull /sdcard/screenshot.png d:/screenshot.png(保存到电脑)

adb录制视频

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
adb shell screenrecord --help
Usage: screenrecord [options] <filename>


Records the device's display to a .mp4 file.


Options:
--size WIDTHxHEIGHT
Set the video size, e.g. "1280x720". Default is the device's main display resolution (if
supported), 1280x720 if not. For best results, use a size supported by the AVC encoder.
--bit-rate RATE
Set the video bit rate, in megabits per second. Default 4Mbps.
--time-limit TIME
Set the maximum recording time, in seconds. Default / maximum is 180.
--rotate
Rotate the output 90 degrees.

--verbose
Display interesting information on stdout.

--help

Show this message.

Recording continues until Ctrl-C is hit or the time limit is reached.

录制

1
adb shell screenrecord /sdcard/test.mp4

视频保存目录可以自己指定,如上面的/sdcard/test.mp4,
命令执行后会一直录制180s,按下ctrl+c可以提前结束录制

设定视频分辨率

对于高分辨率的手机,录制的视频很大,我们分享又不需要这么大的
我们可以设置录制的视频分辨率

1
adb shell screenrecord --size 848*480 /sdcard/test.mp4

设定视频比特率

默认比特率是4M/s,为了分享方便,我们可以调低比特率为2M

1
adb shell screenrecord --bit-rate 2000000 /sdcard/test.mp4

获取视频文件使用adb pull 即可把手机SD卡中视频获取到本地

1
adb pull /sdcard/test.mp4 .

转GIF文件

在Windows下有个不错的软件Free Video to GIF Converter可以把mp4转换成GIF。