简单又实用的高级技巧,带你深入了解Python自动化控制手机
itomcoil 2024-12-07 13:24 100 浏览
大家好,我是皮皮,今天给大家讲讲ADB操作。
前面给大家分享了Airtest,感兴趣的小伙伴,可以前往:手机自动化测试IDE-----Airtest实战篇、手机自动化测试IDE ----- 手把手教你用Airtest模拟器来连接手机、手机自动化测试IDE-----Airtest基本操作方法、手机自动化测试IDE ----- Airtest的安装和IDE控件详解,任君挑选。
私信小编01即可获取大量python学习资源
前言
ADB,中文名安卓调试桥,它是一种功能多样的命令行工具,可用于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令)的访问权限。它是一种客户端-服务器程序,包括以下三个组件:
客户端:用于发送命令。客户端在开发计算机上运行。您可以通过发出 ADB命令来从命令行终端调用客户端。
守护进程:在设备上运行命令。守护进程在每个设备上作为后台进程运行。
服务器:管理客户端和守护进程之间的通信。服务器在开发机器上作为后台进程运行。
通过这个命令行工具我们可以使用它来操作手机上的资源,而且不需要借助Android SDK就可以轻松操控手机里的应用。
一、工作原理
当您启动某个ADB客户端时,客户端会先检查是否有 ADB服务器进程正在运行。如果没有,它将启动服务器进程。服务器在启动后会与本地 TCP 端口 5037 绑定,并监听 ADB 客户端发出的命令 - 所有 ADB客户端均通过端口 5037 与 ADB 服务器通信。
然后,服务器会与所有正在运行的设备建立连接。它通过扫描 5555 到 5585 之间(该范围供前 16 个模拟器使用)的奇数号端口查找模拟器。服务器一旦发现ADB守护进程 (ADBD),便会与相应的端口建立连接。请注意,每个模拟器都使用一对按顺序排列的端口 - 用于控制台连接的偶数号端口和用于ADB 连接的奇数号端口。
二、下载ADB
这里小编已经给大家准备好了,下载地址:
https://u062.com/file/7715018-452566390 ,
如果你想在任何文件目录使用ADB的话,必须把ADB的安装目录加入环境变量,然后我们可以使用ADB的界面工具,它可以很直观方便的执行ADB的命令并显示结果,
这个工具就是APKInstaller,下载地址: https://u062.com/file/7715018-452566398 ,如图:
三、基本操作
1.设备的启动关闭操作
这里我选择连接模拟器,看看连接有什么区别。
1).连接设备
adb connect 127.0.0.1:62001#连接一个模拟器
2).启动服务
adb start-server#开启服务
3).查看设备
adb devices #查看设备信息
adb version #设备版本
adb help #帮助文档
adb get-state #查看设备状态 device(正常连接),offline(连接异常),unknown(没有连接)
adb get-serialno #获取序列号
adb get-devpath #获取设备路径
adb shell cat /system/build.prop #获取设备名称
4).操作多个设备
adb —a 监听所有网络接口,而不仅仅是localhost
-d 使用USB设备(如果多个设备连接错误)
-e 使用TCP / IP设备(如果可用多个TCP / IP设备错误)
-s 使用给定的序列号(覆盖ANDROID_SERIAL)
-t 使用给定设备ID
-h adb服务器主机名称(默认= localhost)
-p adb服务器的端口(默认= 5037)
-l 监听来自套接字的adb服务器(默认= tcp: localhost: 5037)
例如:adb -s 127.0.0.1:62001 shell
这样我们呢就进入了模拟器的Shell界面了。
5).关闭服务
adb kill-server
6).设置监听TCP/IP的端口
adb tcpip 5555
7).断开连接
adb disconnect 127.0.0.1:62001
8).关机与重启
adb reboot #设备重启
adb shutdown #设备关机
9).Root权限
adb root # 正常输出:restarting adbd as root
adb unroot # 取消root权限
10).刷机模式
adb reboot bootloader #重启到bootloader,即刷机模式
adb reboot recovery #重启到recovery,即恢复模式
adb sideload <path-to-update.zip>#更新系统
11).命令转载
adb wait-for-device # 在模拟器/设备连接之前把命令转载在adb的命令器中
2.设备应用操作
1).查看应用
adb shell pm list packages #所有应用
adb shell pm list package -f #
adb shell pm list packages -s #系统应用
adb shell pm list packages -3 #三方应用
adb shell pm list packages | findstr bluetooth #过滤应用 管道符进行搜索,Linux下使用grep
2).查看应用的Activity信息
adb shell dumpsys package com.android.bluetooth #查看系统应用蓝牙的包名信息
adb shell dumpsys activity activities #查看所有的活动程序包名
adb shell dumpsys activity | findstr mFocusedActivity #查看当前重启的是哪个包
adb shell dumpsys activity top | findstr activity #查找设备活动程序的父窗口
3).清除应用数据和缓存
adb shell pm clear
3.安装卸载应用
adb install D:/aa.apk #安装在电脑上的apk
adb install -r D:/aa.apk#覆盖安装 保留数据和缓存文件 -g 授予所有运行时权限
adb shell pm install /scard/picture#安装在手机上的apk
adb uninstall -k 包名
在模拟器或者真机中需要使用 -s来指定
4.日志
adb logcat#查看日志
adb logcat -v time #打印详情日志,会记录当前的所有操作行为以及产生的结果,默认持续监听,按下Ctrl+c即可结束
adb logcat -v time >D:\log.txt #保存日志到电脑
adb logcat -f /sdcard/1.txt #保存日志到手机
adb logcat | findstr com.android.bluetooth #保存指定包名的日志
adb logcat -c #清除之前的日志输出
adb logcat | findstr ActivityManager #查看当前正在运行的Activity
adb logcat | findstr Displayed #查看当前正在运行的Activity
adb bugreport #查看bug报告
adb logcat -b radio #无线通讯的日志
adb shell dmesg #内核日志
5.设备文件操作
#把电脑上的文件传到手机储存卡中
adb push C:\Users\Administrator\Desktop\1.gif /sdcard/
#把手机存储卡里的文件传到电脑
adb pull /sdcard/1.gif C:\Users\Administrator\Desktop\
6.截屏,录屏
adb shell screencap /sdcard/1.png #当前窗口截屏保存到手机
adb shell /system/bin/screencap -p /sdcard/2.png
adb exec-out screencap -p >1.png #截图保存到电脑
adb shell screenrecord >1.mp4 #屏幕录像,Ctrl+c停止录制
--size #视频大小
--bit-rate #比特率
--time-limit #持续时间
--verbose #命令行显示log信息
注:模拟器和安卓4.4以下版本不支持录屏
7.Shell
Shell里有很多命令,我们简单列举下:
命令功能
cat显示文件内容
cd切换目录
chmod改变文件的存取模式/访问权限
df查看磁盘空间使用情况
grep过滤输出
kill杀死指定 PID 的进程
ls列举目录内容
mount挂载目录的查看和管理
mv移动或重命名文件
ps查看正在运行的进程
rm删除文件
top查看进程的资源占用情况
1).进入退出
adb shell #进入shell 返回$ 则没有root权限 #有root权限
exit #退出shell
2).设备的相关信息
adb shell getprop ro.build.version.release #安卓系统版本
adb shell getprop ro.product.model #查看设备型号
adb shell cat /sys/class/net/wlan0/address #查看MAC地址
adb shell wm size #设备屏幕分辨率
adb shell wm size 400X654 #设置屏幕分辨率
adb shell wm size reset #恢复原屏幕分辨率
adb shell wm density #设备屏幕密度
adb shell wm density 100 #修改屏幕密度为100dpi
adb shell wm density reset #恢复原屏幕密度
adb shell wm overscan 10,20,30,100 #显示区域
adb shell wm overscan reset #恢复原显示区域
adb shell dumpsys window displays #显示屏参数
adb shell service list #查看后台services信息
adb shell settings put global adb_enabled 0 #关闭 USB 调试模式
adb shell uiautomator dump #获取当前界面的控件信息
adb shell ime list -s #设备上的输入法
3).进程
adb shell ps #查看手机正在运行的进程 adb shell ps | findstr bluetooth
adb shell ps -x pid #查看指定pid的进程状态信息
adb shell kill pid #根据进程号杀进程
adb shell procrank #杀进程
adb shell start adbd #启动守护进程
adb shell stop adbd #关闭守护进程
4).性能分析
adb shell cat /proc/cpuinfo #获取CPU序列号
adb shell cat /proc/meminfo #查看当前内存占用
adb shell cat /proc/iomem #查看IO内存分区
adb remount #将system分区重新挂载为可读写分区
adb shell dumpsys meminfo bluetooth #查看蓝牙占用的内存
adb shell dumpsys cpuinfo | findstr bluetooth #获取CPU
adb shell top #查看实时资源占用情况
adb shell top -n 1 | findstr bluetooth #刷新一次内存信息,然后返回蓝牙内存占用
adb shell top #查看设备cpu和内存占用情况
adb shell top -m 6 #查看占用内存前6的app
adb shell dumpsys gfxinfo bluetooth #获取流畅度相关
adb shell netcfg #查看设备的网络连接情况
adb shell ifconfig wlan0 #获取wlan0的IP地址和子网掩码
5).文件操作
adb shell ls #列出目录下的文件和文件夹
adb shell cd sys #切换当前目录为sys
adb shell rename 旧文件名 新文件名 #重命名文件名
adb shell rm /sys/1.apk #删除指定目录下的文件
adb shell rm -r #删除指定目录下的文件夹及其子目录
adb shell mv 旧文件名 新文件名 #移动文件
adb shell chmod 777 1.jpg #设置文件权限
adb shell mkdir 文件夹名 #新建文件夹
adb shell cat 文件 #查看文件内容
adb shell cat /data/misc/wifi/*.conf #查看WiFi密码
6).按键
adb shell input keyevent 3 # HOME 键
adb shell input keyevent 4 # 返回键
adb shell input keyevent 5 # 拨号
adb shell input keyevent 6 # 挂断
adb shell input keyevent 24 # 音量+
adb shell input keyevent 25 # 音量-
adb shell input keyevent 26 # 电源键
adb shell input keyevent 27 # 拍照
adb shell input keyevent 64 # 打开浏览器
adb shell input keyevent 82 # 菜单键
adb shell input keyevent 85 # 播放/暂停
adb shell input keyevent 86 # 停止播放
adb shell input keyevent 87 # 播放下一首
adb shell input keyevent 88 # 播放上一首
adb shell input keyevent 122 #移动光标到行首或列表顶部
adb shell input keyevent 123 #移动光标到行尾或列表底部
adb shell input keyevent 126 # 恢复播放
adb shell input keyevent 127 # 暂停播放
adb shell input keyevent 164 # 静音
adb shell input keyevent 176 # 打开系统设置
adb shell input keyevent 187 # 切换应用
adb shell input keyevent 207 # 打开联系人
adb shell input keyevent 208 # 打开日历
adb shell input keyevent 209 # 打开音乐
adb shell input keyevent 210 # 打开计算器
adb shell input keyevent 220 # 降低屏幕亮度
adb shell input keyevent 221 # 提高屏幕亮度
adb shell input keyevent 223 # 休眠
adb shell input keyevent 224 # 点亮屏幕
adb shell input keyevent 231 # 打开语音助手
adb shell input keyevent 276 # 如果没有 wakelock 则让系统休眠
7).点击,滑动屏幕
adb shell input tap 100 300 #在(100,300)处点击
adb shell input swipe 100 1200 100 200 #上滑
adb shell input swipe 100 200 100 1200 #下滑
8).输入
adb shell input text hello #输入hello
9).电池
adb shell dumpsys battery
10).设备ID
adb shell settings get secure android_id
11).无线网络
在操作前必须获得Root权限。
adb shell svc wifi enable #开启WiFi
adb shell svc wifi disable #关闭WiFi
8.端口转发
adb forward tcp:60 tcp:70 #将60端口转到70端口
adb forward tcp:60 local:logd # 将60端口转到local:logd的转发
9.Activity 管理器
adb shell am start -n activity路径 #启动某一个activity
adb shell am start -a android.intent.action.VIEW -d www.baidu.com#启动默认浏览器打开一个网页
adb shell am start -n com.android.camera/.Camera #启动相机
adb shell am start -a android.intent.action.CALL -d tel:10086#启动拨号10086
adb shell am startservice -n 服务 #开启服务
adb shell am stopservice 服务 #停止服务
adb shell am force-stop bluetooth #杀死蓝牙进程
adb shell am kill 进程号 #杀掉进程
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED #向所有组件广播设备启动完毕
10.调用软件包管理器
adb shell pm list permissions #查看权限
adb shell pm list permission-groups #输出所有已知的权限组
adb shell pm list permissions -d -g -f #查看系统危险权限并按组输出所有信息
adb shell pm list instrumentation#列出所有测试软件包,-f列出测试软件包的APK文件
adb shell pm path com.android.bluetooth #查看软件安装路径
adb shell pm list features #输出系统的所有功能
adb shell pm list libraries #输出当前设备支持的所有库
adb shell pm list users #输出系统中的所有用户
adb shell pm enable ** #启用给定的软件包或组件(写为“package/class”)
adb shell pm disable ** #停用给定的软件包或组件(写为“package/class”)
adb shell pm get-max-users #输出设备支持的最大用户数
11.备份
adb backup -all #备份所有数据
12.压力测试Monkey
格式:adb shell monkey -v -p your.package.name 500
adb shell monkey -v -p com.tencent.weishi 500
四、总结
以上就是ADB的全部内容了,通过对ADB的了解我相信大家应该能通过Python编程做一个简易版的自动化工具了,不再依靠Airtest。
- 上一篇:如何开始使用 Python?
- 下一篇:macos上独立配置python环境
相关推荐
- Python Qt GUI设计:将UI文件转换Python文件三种妙招(基础篇—2)
-
在开始本文之前提醒各位朋友,Python记得安装PyQt5库文件,Python语言功能很强,但是Python自带的GUI开发库Tkinter功能很弱,难以开发出专业的GUI。好在Python语言的开放...
- Connect 2.0来了,还有Nuke和Maya新集成
-
ftrackConnect2.0现在可以下载了--重新设计的桌面应用程序,使用户能够将ftrackStudio与创意应用程序集成,发布资产等。这个新版本的发布中还有两个Nuke和Maya新集成,...
- Magicgui:不会GUI编程也能轻松构建Python GUI应用
-
什么是MagicguiMagicgui是一个Python库,它允许开发者仅凭简单的类型注解就能快速构建图形用户界面(GUI)应用程序。这个库基于Napari项目,利用了Python的强大类型系统,使得...
- Python入坑系列:桌面GUI开发之Pyside6
-
阅读本章之后,你可以掌握这些内容:Pyside6的SignalsandSlots、Envents的作用,如何使用?PySide6的Window、DialogsandAlerts、Widgets...
- Python入坑系列-一起认识Pyside6 designer可拖拽桌面GUI
-
通过本文章,你可以了解一下内容:如何安装和使用Pyside6designerdesigner有哪些的特性通过designer如何转成python代码以前以为Pyside6designer需要在下载...
- pyside2的基础界面(pyside2显示图片)
-
今天我们来学习pyside2的基础界面没有安装过pyside2的小伙伴可以看主页代码效果...
- Python GUI开发:打包PySide2应用(python 打包pyc)
-
之前的文章我们介绍了怎么使用PySide2来开发一个简单PythonGUI应用。这次我们来将上次完成的代码打包。我们使用pyinstaller。注意,pyinstaller默认会将所有安装的pack...
- 使用PySide2做窗体,到底是怎么个事?看这个能不能搞懂
-
PySide2是Qt框架的Python绑定,允许你使用Python创建功能强大的跨平台GUI应用程序。PySide2的基本使用方法:安装PySide2pipinstallPy...
- pycharm中conda解释器无法配置(pycharm安装的解释器不能用)
-
之前用的好好的pycharm正常配置解释器突然不能用了?可以显示有这个环境然后确认后可以conda正在配置解释器,但是进度条结束后还是不成功!!试过了pycharm重启,pycharm重装,anaco...
- Conda使用指南:从基础操作到Llama-Factory大模型微调环境搭建
-
Conda虚拟环境在Linux下的全面使用指南:从基础操作到Llama-Factory大模型微调环境搭建在当今的AI开发与数据分析领域,conda虚拟环境已成为Linux系统下管理项目依赖的标配工具。...
- Python操作系统资源管理与监控(python调用资源管理器)
-
在现代计算环境中,对操作系统资源的有效管理和监控是确保应用程序性能和系统稳定性的关键。Python凭借其丰富的标准库和第三方扩展,提供了强大的工具来实现这一目标。本文将探讨Python在操作系统资源管...
- 本地部署开源版Manus+DeepSeek创建自己的AI智能体
-
1、下载安装Anaconda,设置conda环境变量,并使用conda创建python3.12虚拟环境。2、从OpenManus仓库下载代码,并安装需要的依赖。3、使用Ollama加载本地DeepSe...
- 一文教会你,搭建AI模型训练与微调环境,包学会的!
-
一、硬件要求显卡配置:需要Nvidia显卡,至少配备8G显存,且专用显存与共享显存之和需大于20G。二、环境搭建步骤1.设置文件存储路径非系统盘存储:建议将非安装版的环境文件均存放在非系统盘(如E盘...
- 使用scikit-learn为PyTorch 模型进行超参数网格搜索
-
scikit-learn是Python中最好的机器学习库,而PyTorch又为我们构建模型提供了方便的操作,能否将它们的优点整合起来呢?在本文中,我们将介绍如何使用scikit-learn中的网格搜...
- 如何Keras自动编码器给极端罕见事件分类
-
全文共7940字,预计学习时长30分钟或更长本文将以一家造纸厂的生产为例,介绍如何使用自动编码器构建罕见事件分类器。现实生活中罕见事件的数据集:背景1.什么是极端罕见事件?在罕见事件问题中,数据集是...
- 一周热门
- 最近发表
-
- Python Qt GUI设计:将UI文件转换Python文件三种妙招(基础篇—2)
- Connect 2.0来了,还有Nuke和Maya新集成
- Magicgui:不会GUI编程也能轻松构建Python GUI应用
- Python入坑系列:桌面GUI开发之Pyside6
- Python入坑系列-一起认识Pyside6 designer可拖拽桌面GUI
- pyside2的基础界面(pyside2显示图片)
- Python GUI开发:打包PySide2应用(python 打包pyc)
- 使用PySide2做窗体,到底是怎么个事?看这个能不能搞懂
- pycharm中conda解释器无法配置(pycharm安装的解释器不能用)
- Conda使用指南:从基础操作到Llama-Factory大模型微调环境搭建
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)