在Python中使用ADB(Android Debug Bridge)命令,可以通过以下几种方式实现:
1. 使用`os`模块:
import os
os.system('adb devices') 列出所有连接的设备
os.system('adb shell wm size') 获取屏幕分辨率
os.system('adb shell cat /proc/cpuinfo') 获取CPU信息
2. 使用`subprocess`模块:
import subprocess
def adb_devices():
cmd = 'adb devices'
c_line = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
if 'List of devices attached' in c_line:
return c_line.split('\n')[1:] 返回设备ID列表
return None
3. 使用第三方库`enhanced-adb`:
from adb import ADBDevice
device = ADBDevice()
device.connect()
print(f'Connected to {device.device_id}')
print(f'Device model: {device.model}')
print(f'Device product: {device.product}')
4. 使用第三方库`pure-python-adb`:
from ppadb.client import Client as AdbClient
client = AdbClient(host='127.0.0.1', port=5037)
devices = client.devices()
if devices:
device = devices
print('Device connected:', device.serial)
client.install_app('path/to/your/app.apk')
5. 使用第三方库`adb-shell`:
import adb_shell
shell = adb_shell.adb_device.AdbDeviceTcp('127.0.0.1', 5555)
shell.connect()
print(shell.execute('dumpsys activity | grep mFocusedActivity'))
请根据您的需求选择合适的方法。