1.判断是否锁屏
def is_screen_lock(id):
# ----------------------
# 检测屏幕是否被锁,不同于屏幕点亮, 判断亮屏请使用 adbutils.is_screen_on
# ----------------------
try:
cmd_1 = os.popen('adb -s ' + id + ' shell dumpsys window policy^|grep isStatusBarKeyguard').read()
cmd_2 = os.popen('adb -s ' + id + ' shell dumpsys window policy^|grep mShowingLockscreen').read()
if "isStatusBarKeyguard=true".strip() in cmd_1:
return True
elif "mShowingLockscreen=true".strip() in cmd_2:
return True
elif len(cmd_1) == 0 or len(cmd_2) == 0:
command = 'adb -s ' + id + " shell dumpsys window policy"
result = os.popen(command)
lines = result.readlines()
loc_flag = 0
for i in range(len(lines)):
# print('KeyguardServiceDelegate'.strip())
if 'KeyguardServiceDelegate'.strip() in lines[i]:
loc_flag = i + 1
# print(lines[i])
# print(lines[loc_flag])
if 'showing=true' in lines[loc_flag] and loc_flag != 0:
return True
else:
return False
else:
return False
except Exception as e:
print('获取手机lock状态异常',e)
return False
2.判断是否点亮
####第一种方法
def is_screenState(id):
try:
cmd = 'adb -s ' + id + ' shell dumpsys power | find "Display Power: state="'
res = os.popen(cmd).read()
print(res)
if "state=ON" in res:
return True
else:
return False
except Exception as e:
print('获取手机屏幕点亮状态异常',e)
return False
####第二种方法
def is_screenState_bake(id):
try:
command = 'adb -s ' + id + " shell dumpsys window policy"
result = os.popen(command)
lines = result.readlines()
#print(lines)
loc_flag = 0
for i in range(len(lines)):
#print('KeyguardServiceDelegate'.strip())
if 'screenState'.strip() in lines[i]:
loc_flag = i
#print(lines[i])
#print(lines[loc_flag])
if 'screenState=SCREEN_STATE_ON' in lines[loc_flag] or 'screenState=2' in lines[loc_flag]:
return True
else:
return False
except Exception as e:
print('获取手机屏幕点亮状态异常',e)
return False