74 lines
1.6 KiB
Python
74 lines
1.6 KiB
Python
import sys
|
|
import os
|
|
import time
|
|
|
|
import uiautomator2 as u2
|
|
|
|
|
|
def kill_all_apps():
|
|
try:
|
|
d = u2.connect()
|
|
except Exception as e:
|
|
print(f"设备连接失败: {e}")
|
|
return False
|
|
|
|
print("返回桌面...")
|
|
try:
|
|
d.press("home")
|
|
except Exception as e:
|
|
print(f"按 Home 键失败: {e}")
|
|
return False
|
|
|
|
time.sleep(0.5)
|
|
|
|
print("打开快捷设置面板...")
|
|
opened = False
|
|
try:
|
|
d.open_quick_settings()
|
|
opened = True
|
|
except Exception:
|
|
try:
|
|
d.open_notification()
|
|
opened = True
|
|
except Exception as e:
|
|
print(f"打开快捷设置/通知面板失败: {e}")
|
|
|
|
if not opened:
|
|
return False
|
|
|
|
time.sleep(0.5)
|
|
|
|
print("查找“一键清理”按钮并点击...")
|
|
btn = None
|
|
for _ in range(3):
|
|
btn = d(text="一键清理")
|
|
if btn.exists:
|
|
break
|
|
btn = d(resourceId="com.android.systemui:id/statebutton3")
|
|
if btn.exists:
|
|
break
|
|
time.sleep(0.5)
|
|
|
|
if not btn or not btn.exists:
|
|
print("未找到“一键清理”按钮,请确认系统界面支持此功能。")
|
|
return False
|
|
|
|
try:
|
|
btn.click()
|
|
time.sleep(0.5)
|
|
print("已点击“一键清理”,正在结束后台应用。")
|
|
return True
|
|
except Exception as e:
|
|
print(f"点击“一键清理”失败: {e}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
success = kill_all_apps()
|
|
if not success:
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|