python如何群控手机_python调用adb脚本来实现群控安卓手机初探

[Python] 纯文本查看 复制代码# !/usr/bin/python

# -*- coding:utf-8 -*-

import os

runapp_name = "com.tencent.mobileqq/.activity.SplashActivity"

def getDevicesAll():

#获取所有的设备列表

devices = []

try:

for dName_ in os.popen("adb devices"):

if "\t" in dName_:

if dName_.find("emulator") < 0:

devices.append(dName_.split("\t")[0])

devices.sort(cmp=None, key=None, reverse=False)

except:

pass

print("设备名称: %s 总数量:%s台" % (devices, len(devices)))

return devices

def find_top_app(devices):

# 循环输入顶层应用

for dName in devices:

try:

out = os.popen('adb shell "dumpsys activity | grep "mFocusedActivity""').read()

print(dName+"的当前顶层应用是:"+out)

except:

print(dName + "关闭失败")

def runtest(devices):

# 打开应用

for dName in devices:

try:

os.popen("adb -s " + dName +" shell am start -n "+runapp_name)

print("启动成功")

except:

print(dName+"打开失败")

if __name__=="__main__":

while True:

try:

devices = getDevicesAll()

except:

print("获取设备出错")

res = input("输入1查看当前的顶层app;2启动qq:")

if int(res)==1:

try:

find_top_app(devices)

except:

print("启动错误")

elif int(res)==2:

try:

runtest(devices)

except:

print("启动错误")

你可能感兴趣的:(python如何群控手机)