测试-点击屏幕避免灭屏

import os
import time

num = 0
cdx = False
isf = True
ab = ''

while True:
    dvs = os.popen("adb devices").readlines()
    for ss in dvs:
        ss = ss.strip('\n')
        if 'List of devices' not in ss and len(ss) > 6 and 'offline' not in ss:
            dv = ss.split('\t')[0]
            if isf:
                if not ab:
                    ab = input('is send key 26?\n')
                if ab:
                    os.system("adb -s %s shell input keyevent 26" % dv)
                    time.sleep(1)
            print(dv)
            os.system("adb -s %s shell input tap 1130 36" % dv)
            cdx = True
            time.sleep(1)
    isf = False
    if cdx:
        num += 1
        sd = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        print('%s —— 次数%d' % (sd, num))
        time.sleep(4.6 * 60)
    else:
        print('没有连接设备')
        input()
        break
    cdx = False

修改版

import os
import time

num = 0
cdx = False

while True:
    dvs = os.popen("adb devices").readlines()
    for ss in dvs:
        ss = ss.strip('\n')
        if 'List of devices' not in ss and len(ss) > 6 and 'offline' not in ss:
            dv = ss.split('\t')[0]
            print(dv)
            dsw = os.popen("adb -s %s shell dumpsys window" % dv).read()
            if 'mAwake=false' in dsw and 'mScreenOnEarly=false' in dsw and 'mScreenOnFully=false' in dsw:
                os.system("adb -s %s shell input keyevent 26" % dv)
                time.sleep(1)
            else:
                os.system("adb -s %s shell input tap 1130 36" % dv)
                time.sleep(1)
            cdx = True
    if cdx:
        num += 1
        sd = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        print('%s —— 次数%d' % (sd, num))
        time.sleep(3.9 * 60)
    else:
        print('没有连接设备')
        input()
        break
    cdx = False

最终版:

import os
import subprocess
import time

num = 0
cdx = False


def run_silently(cmd: str) -> str:
    with os.popen(cmd) as fp:
        bf = fp._stream.buffer.read()
    try:
        return bf.decode().strip()
    except UnicodeDecodeError:
        return bf.decode('gbk').strip()


while True:
    dvs = os.popen("adb devices").readlines()
    for ss in dvs:
        ss = ss.strip('\n')
        if 'List of devices' not in ss and len(ss) > 6 and 'offline' not in ss:
            dv = ss.split('\t')[0]
            print(dv)
            # dsw = run_silently("adb -s %s shell dumpsys window" % dv)

            # process = os.popen("adb -s %s shell dumpsys window" % dv)
            # dsw = process.read()
            # process.close()

            p = subprocess.Popen("adb -s %s shell dumpsys window" % dv, stdout=subprocess.PIPE)
            result = p.communicate()
            dsw = result[0].decode('utf-8')

            if 'mAwake=false' in dsw and 'mScreenOnEarly=false' in dsw and 'mScreenOnFully=false' in dsw:
                os.system("adb -s %s shell input keyevent 26" % dv)
                time.sleep(1)
            else:
                os.system("adb -s %s shell input tap 1130 36" % dv)
                time.sleep(1)
            cdx = True
    if cdx:
        num += 1
        sd = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        print('%s —— 次数%d' % (sd, num))
        time.sleep(3.9 * 60)
    else:
        print('没有连接设备')
        input()
        break
    cdx = False

你可能感兴趣的:(python3,python)