check_app.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import subprocess
  2. from tools import loggerKit
  3. # 检查app运行状态
  4. def is_app_running(device_id, package_name):
  5. # 执行 adb shell pidof 命令检测应用程序是否运行
  6. command = ['adb', '-s', device_id, 'shell', 'pidof', package_name]
  7. result = subprocess.run(command, capture_output=True, text=True)
  8. # 检查命令执行结果
  9. if result.returncode == 0 and result.stdout.strip().isdigit():
  10. return True
  11. else:
  12. return False
  13. # 检查app运行状态
  14. def check_app_running(device_id, package_name):
  15. # 执行 adb shell pidof 命令检测应用程序是否运行
  16. try:
  17. command = ['adb', '-s', device_id, 'shell', 'pidof', package_name]
  18. result = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
  19. return result.strip().isdigit()
  20. except subprocess.CalledProcessError as e:
  21. loggerKit.error("Failed to check if app {0} is running on device {1}, 异常信息:{2}", package_name, device_id,
  22. e.output)
  23. return True
  24. # 检查app运行状态
  25. # check app运行状态
  26. def check_app_status(device_id, package_name):
  27. try:
  28. # Execute adb command to check if the app process is running
  29. command = ['adb', '-s', device_id, 'shell', 'pidof', package_name]
  30. result = subprocess.run(command, capture_output=True, text=True, check=True)
  31. # Check the return code to determine if the app process is running
  32. if result.returncode == 0:
  33. return True
  34. else:
  35. loggerKit.error("app {0} 未运行 on device {1}", package_name, device_id)
  36. return False
  37. except subprocess.CalledProcessError as e:
  38. loggerKit.error("Failed to check if app {0} is running on device {1}, 异常信息:{2}", package_name, device_id,
  39. e)
  40. return False