os.system("adb shell input keyevent KEYCODE_POWER")
or
os.system('adb shell input keyevent 26')
# 屏幕从(500,700)滑动到(500,50),延迟200号,毫秒
os.system("adb -s shell input swipe 500 700 500 50 200" )
# 输入密码
os.system('adb shell input text 1111')
import datetime,os
# 手机电源开关
def power_switch(device='',p_num='0号'):
hour = datetime.datetime.today().strftime('%H')
adb_msg = os.popen('adb -s %s shell dumpsys window policy' % device)
phone_msg = adb_msg.buffer.read().decode(encoding='utf-8')
# 10:00~21:00,使手机处于亮屏和解锁状态
if '10' <=hour <= '21':
if 'mAwake=true' in phone_msg and 'mInputRestricted=false' in phone_msg:
print('%s手机,亮屏时段,####已亮屏,已解锁####'%p_num)
logger.info('%s手机,亮屏时段,####已亮屏,已解锁####'%p_num)
elif 'mAwake=true' in phone_msg:
print('%s手机,亮屏时段,****已亮屏,执行解锁****'%p_num)
logger.info('%s手机,亮屏时段,****已亮屏,执行解锁****' % p_num)
# 屏幕从(500,700)滑动到(500,50),延迟200号,毫秒
os.system("adb -s %s shell input swipe 500 700 500 50 200" % device)
else:
print('%s手机,亮屏时段,====执行亮屏和解锁====' % p_num)
logger.info('%s手机,亮屏时段,====执行亮屏和解锁====' % p_num)
os.system("adb -s %s shell input keyevent KEYCODE_POWER" % device)
# 屏幕从(500,700)滑动到(500,50),延迟200号,毫秒
os.system("adb -s %s shell input swipe 500 700 500 50 200" % device)
else:
# 处于息屏状态
if 'mAwake=true' in phone_msg:
print('%s手机,非亮屏时段,====已亮屏,关闭亮屏====' % p_num)
logger.info('%s手机,非亮屏时段,====已亮屏,关闭亮屏====' % p_num)
os.system("adb -s %s shell input keyevent KEYCODE_POWER" % device)
else:
print('%s手机,非亮屏时段,====未亮屏====' % p_num)
logger.info('%s手机,非亮屏时段,====未亮屏====' % p_num)
mFocusedActivity = os.popen("adb shell dumpsys activity | grep 'mFocusedActivity' | awk '{print $4}' | awk -F '/' '{print $1}'").read().strip('\n')
if mFocusedActivity == 'com.eg.android.AlipayGphone':
print("APP已启动,停止APP,等待重新启动")
os.system('adb shell am force-stop com.eg.android.AlipayGphone')
time.sleep(1)
print("启动app")
os.system('adb shell am start -n com.eg.android.AlipayGphone/com.eg.android.AlipayGphone.AlipayLogin activity')
以上操作需要adb环境已经搭建好,使用以下命令查看连接设备
C:\Users\enmonster>adb devices
List of devices attached
97dc2d9 device
想获取某个APP名称用于启动时,手机先打开APP,然后执行以下命令
adb shell dumpsys window windows |grep "Current"