测试-android通过adb点击屏幕避免灭屏

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

最最终版:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# version v1.1

import os
import subprocess
import time

num = 0
tnum = 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)
            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)
            else:
                os.system("adb -s %s shell input tap 1130 36" % dv)
            cdx = True
            time.sleep(0.36)
    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:
        tnum += 1
        print('没有连接设备')
        if tnum == 69:
            print('------------------------')
            tnum = 0
            it = input()
            if not it:
                break
        else:
            time.sleep(1 * 60)
    cdx = False

最最终版2:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# version v1.1

import os
import subprocess
import time

num = 0
tnum = 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)
            p = subprocess.Popen("adb -s %s shell dumpsys window" % dv, stdout=subprocess.PIPE)
            result = p.communicate()
            dsw = result[0].decode('utf-8')
            bb = 'mAwake=false' in dsw and 'mScreenOnEarly=false' in dsw and 'mScreenOnFully=false' in dsw
            if bb:
                p2 = subprocess.Popen("adb -s %s shell dumpsys deviceidle" % dv, stdout=subprocess.PIPE)
                result2 = p2.communicate()
                dsw2 = result2[0].decode('utf-8')
                bb = 'mScreenOn=false' in dsw2
            if bb:
                os.system("adb -s %s shell input keyevent 26" % dv)
            else:
                os.system("adb -s %s shell input tap 1130 36" % dv)
            cdx = True
            time.sleep(0.36)
    if cdx:
        num += 1
        sd = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        print('%s —— 次数%d' % (sd, num))
        time.sleep(3 * 60)
    else:
        tnum += 1
        print('没有连接设备')
        if tnum == 69:
            print('------------------------')
            tnum = 0
            it = input()
            if not it:
                break
        else:
            time.sleep(1 * 60)
    cdx = False

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值