这篇文章主要介绍“python如何使用电脑操控安卓手机”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python如何使用电脑操控安卓手机”文章能帮助大家解决问题。

一、电脑下载并安装SDK Platform Tools

下载后的文件:platform-tools_r30.0.4-windows.zip(大约12M)

接着解压文件到指定目录

解压后的路径与文件,接着为工具目录添加系统环境变量

验证安装结果

#执行命令adbversion

手机连接电脑USB后执行adb devices 查看手机连接状态

查询已连接设备/模拟器:adb devices

此处连接手机,需要手机在开发者模式开启USB调试功能。顺便也开启模拟按键功能,后面会用到。

该命令经常出现以下问题:

offline —— 表示设备未连接成功或无响应;

device —— 设备已连接;

no device —— 没有设备/模拟器连接;

List of devices attached 设备/模拟器未连接到 adb 或无响应

简单获取手机屏幕坐标的方法

方法1、进入手机的开发者模式,打开在手机上实时显示坐标的功能,长按屏幕位置自动显示坐标

方法2、使用android adb shell命令获取

#截屏到手机adbshellscreencap/sdcard/screen.png#将手机上刚才的截图上传到电脑adbpull/sdcard/screen.png/Users/Administrator/Desktop/screen.png

从原始图片上,使用PS扣出自己想要的局部图片,然后使用下面的代码获取局部图片在原始图片上的坐标
不会使用PS,可以直接使用截图在原始图片上截图,但是没PS精细。

importaircvasac#根据图片在图片上查找坐标#imgsrc=原始图像,imgobj=待查找的图片,confidence=设置匹配系数defmatchImg(imgsrc,imgobj,confidence=0.2):imsrc=ac.imread(imgsrc)imobj=ac.imread(imgobj)match_result=ac.find_all_template(imsrc,imobj,confidence)returnmatch_resultif__name__=='__main__':p=matchImg("C:\Users\Administrator\Desktop\screen.png","C:\Users\Administrator\Desktop\daicha.png")print(p)使用python操控手机

importosimporttimedefexecute(cmd):adbstr='adbshell{}'.format(cmd)print(adbstr)os.system(adbstr)if__name__=='__main__':whileTrue:#点击位置一execute("inputtap3502200")time.sleep(3)#点击位置二execute("inputtap970135")time.sleep(5)android adb shell 常用命令

android adb shell官方命令(英文)https://adbshell.com/

以下命令来源:

1.模拟点击

adbshellinputtap100100

2.滑动

adbshellinputswipex1y1x2y2adbinputtouchscreenswipex1y1x2y2100adbshellinputswipe100100400100300#左往右adbshellinputswipe400100100100300#右往左adbshellinputswipe100100100400300#上往下adbshellinputswipe100400100100300#下往上adbshellinputswipe100100400400300#上往下斜adbshellinputswipe400400100100300#下往上斜

3.长按

adbshellinputswipe1001001001001000//在100100位置长按1000毫秒adbshellinputswipe367469367469800

4.打印所有包名

adbshellpmlistpackages➜~adbshellpmlistpackagespackage:com.huawei.floatMmspackage:com.android.defcontainerpackage:com.tencent.mm

5.打印制定包的apk路径

adbshellpmpathcom.android.phone➜~adbshellpmpathcom.huawei.android.launcherpackage:/system/app/HwLauncher6.apk

6.删除制定包

adbshellpmclearcom.test.abc

7.截图

adbshellscreencap/sdcard/screen.pngadbpull/sdcard/screen.png#下载到本地

8.获取被点击的位置信息

adbshellgetevent>/dev/input/event03393e1/dev/input/event0114a1/dev/input/event011451/dev/input/event0335406//x坐标/dev/input/event03541083//y坐标/dev/input/event0000/dev/input/event0339ffffffff/dev/input/event0114a0/dev/input/event011450/dev/input/event000getevent-l-c16输出所有event设备的基本信息adddevice1:/dev/input/event2name:"hi6421_on"couldnotgetdriverversionfor/dev/input/mouse0,Notatypewriteradddevice2:/dev/input/event4name:"huawei,touchscreen"adddevice3:/dev/input/event0name:"mhl_rcp_dev"couldnotgetdriverversionfor/dev/input/mice,Notatypewriteradddevice4:/dev/input/event1name:"hisi_gpio_key.14"adddevice5:/dev/input/event3name:"hi3630_hi6401_CARDHeadsetJack"getevent-c10//输出10条信息后退出getevent-l//将type、code、value以对应的常量名称显示

9.打开对应的activity

adbshellamstart-n{包(package)名}/{包名}.{活动(activity)名称}adbshellamstartcom.songheng.eastnews/com.oa.eastfirst.activity.WelcomeActivity

10.获得当前活动窗口的信息,包名以及活动窗体

adbshelldumpsyswindowwindows|grepmCurrent

11.包名管理命令,获得对应包名的对应apk路径

adbshellpmpathcom.migu.lobby

12.使用dumpsys命令可以查看Android手机当前正在运行的Activity

adbshelldumpsysactivityactivities|findstr"Run"

13.使用 uiautomator dump 获取app上的页面元素

adbshelluiautomatordump/data/local/tmp/uidump.xmladbshelluiautomatordump/sdcard/dump.xml

14.下载文件

adbpull/sdcard/demo.mp4

15.上传文件

adbpushtest.apk/sdcard

16.息屏

adbshellinputkeyevent26

17.keyevent

adbshellinputkeyevent20#向下adbshellinputkeyevent4#返回adbshellinputkeyevent3#Homeadbshellinputkeyevent6#挂机adbshellinputkeyevent84#搜索adbshellinputkeyevent26#电源adbshellinputkeyevent24#音量+adbshellinputkeyevent25#音量-

18.输入框输入

adbshellinputtext"ANDROID"

19.利用无线来查看adb shell

>adbtcpip5555连接:>adbconnectIP:5555

20.查看所有已经连接上的设备

adbdevices

21.安装卸载

adbinstall<apk文件路径>adbinstall-r<apk文件路径>通过install命令来安装apk文件,-r参数可以重新安装某个应用并保留应用数据adbinstall-r~/chrome.apk卸载应用:adbuninstall<软件名>adbuninstall-k<软件名>如果加-k参数,为卸载软件但是保留配置和缓存文件adbuninstallcom.android.chrome

22.关机命令

adbshellsureboot-p

关于“python如何使用电脑操控安卓手机”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。