adb 输入命令

项目自带 adb
先进入到项目的adb目录下面:adb shell input
Usage: input [] […]

The sources are:
trackball
joystick
touchnavigation
mouse
keyboard
gamepad
touchpad
dpad
stylus
touchscreen

The commands and default sources are:
text (Default: touchscreen)
keyevent [–longpress] … (Default: keyboard)
tap (Default: touchscreen)
swipe [duration(ms)] (Default: touchscreen)
press (Default: trackball)
roll (Default: trackball)

D:\pycode\xxx\libs\adb\windows>adb shell input tap 299 572 默认是touchscreen

该命令是用于向设备发送一个点击操作的指令,参数是 坐标
adb shell input tap 299 572

https://blog.youkuaiyun.com/good123_2014/article/details/79107765
https://www.jianshu.com/p/c2ad70ba7411

### 使用 Python 发送 ADB 命令至安卓设备 为了在 Python 中通过 ADB 向安卓设备发送命令,可以利用 `subprocess` 模块来调用外部程序。这允许执行任意的 shell 或者批处理命令,并获取其返回的结果。 #### 导入必要的库 首先需要导入用于操作系统的模块以及子进程管理器: ```python import subprocess import os ``` #### 设置 ADB 路径 确保环境变量中已经配置好了 ADB 的路径,或者手动指定 ADB 工具的位置[^2]。如果选择了后者,则可以通过如下方式设置路径: ```python adb_path = os.path.join(os.environ.get("ANDROID_HOME"), "platform-tools", "adb") if not os.path.isfile(adb_path): raise FileNotFoundError(f"ADB executable file does not exist at {adb_path}") ``` #### 构建并执行 ADB 命令 构建想要传递给 ADB 的具体参数列表,之后使用 `subprocess.run()` 方法运行这些指令。下面是一个简单的例子,展示怎样连接到默认的 Android 设备/模拟器并通过它拉取文件内容: ```python def adb_command(command, device_id=None): cmd_list = [adb_path] if device_id is not None: cmd_list.extend(["-s", device_id]) cmd_list.append(command) result = subprocess.run(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: output = result.stdout.decode('utf-8') error = result.stderr.decode('utf-8') if result.returncode != 0 or error: print(f"Error occurred while executing command: {error}") return output.strip() except Exception as e: print(e) return "" ``` 此函数接受两个参数:一个是具体的 ADB 命令字符串;另一个是可选的目标设备 ID(当有多个连接时)。该方法会尝试解析命令的标准输出和错误流,并打印任何可能发生的异常信息。 #### 实际应用场景下的代码片段 假设现在要比较两份位于 `/sdcard/testfile1.txt` 和 `/sdcard/testfile2.txt` 文件的内容一致性,那么就可以这样做: ```python device_file_1_content = adb_command("shell cat /sdcard/testfile1.txt") device_file_2_content = adb_command("shell cat /sdcard/testfile2.txt") print("File contents are equal:", device_file_1_content == device_file_2_content) ``` 上述代码将会读取远程设备上指定位置处的文本数据,并将其转换成字符串形式以便于后续处理或显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值