Android之adb常用命令2

查看应用的cpu和内存占用情况
查看cpu 方法1:

低版本Android(Android N及之前):adb shell top -n 1 | sed -n '4,17p'
高版本ANdroid(Android O及之后):adb shell top -n 1 | sed -n '5,15p'
方法2:
adb shell dumpsys cpuinfo
方法3(不推荐):
adb shell top -m 10 -s cpu -n 2
其中,-m 10选项表示获取前10行数据,-s cpu表示根据cpu排序(其它值为:vss,rss,thr),-n 2表示执行2次。
注意:不同方法可能获取到的cpu数据将会差别很大。瞬时数据以top为准;而cpuinfo取的是一段时间的平均cpu值,而且受其他命令影响大,尤其是dumpsys meminfo,所以一般优先执行dumpsys cpuinfo。建议用busybox top -b -n 1,单次抓取,比系统带的快,计算精度高一位。
查看内存
【Android之adb常用命令2】adb shell dumpsys meminfo com.package
VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
如果需要查看设备系统总内存及占用情况,可以使用adb shell cat /proc/meminfo查看。
截取屏幕(截屏)和 录制屏幕 (录屏)
截屏
方法1(推荐):
执行:adb shell screencap -p /sdcard/1.png 保存图片到设备sdcard
然后再执行adb pull /sdcard/1.png ~/xxx/ 把截图pull到电脑上。
方法2:
Android 7.0及以上:adb shell screencap -p > screen.png
Android 6.0:adb exec-out screencap -p > screen.png 或者 adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png
注意:低版本Android可能用此方法保存的图片是损坏的,因此推荐使用方法一。
录屏
对于Android4.4以上的手机,系统自带了一个命令screenrecord,我们可以很方便的使用。
录屏命令:adb shell screenrecord /sdcard/test.mp4
命令执行后会一直录制180s,按下ctrl+c可以提前结束录制。
通过adb shell screenrecord --help命令我们可以查看到--size,--bit-rate、--bugreport,--time-limit,--verbose,--help
参数--size设定视频分辨率(默认是手机设备的分辨率):
adb shell screenrecord --size 1280*720 /sdcard/test.mp4 # 录屏,保存视频的分辨率为1280*720
参数--bit-rate设定视频比特率(默认4M/s,对应数字:4000000):
adb shell screenrecord --bit-rate 2000000 /sdcard/test.mp4 # 录屏,保存视频的比特率为2M/s
参数–time-limit N设定录制时间:
adb shell screenrecord /sdcard/test.mp4 --time-limit 10 #录屏10s
注意:仅支持mp4视频格式。
input 模拟事件 通过adb shell input 查看相关命令使用方法
Android之adb常用命令2
文章图片
text 输入文本
keyevent 功能键
tap 点击
swipe 滑动(长按)
draganddrop 拖拽
press 发送点击按压命令
roll 发送滚动命令
keyevent事件
发送事件 adb shell input keyevent key_code
发送文本信息 adb shell input text "xxxxxx"
adb shell input keyevent 3 #点击 home键操作
adb shell input keyevent 4 #点击返回键操作
adb shell input keyevent 8 # for key '1'
adb shell input keyevent 29 #for key 'A'
adb shell input text "hello" #发送文本hello
其他key_code参照以下文章。
https://www.jianshu.com/p/d330ce591008

    推荐阅读