使用python代码执行adb命令
1.统计app 启动时间使用命令
*:获取app包名和启动页名称可查看 常用的adb命令
import subprocess class AppStart: def __init__(self,package_name,activity_name): """ :param package_name: app的包名 :param activity_name: 启动页的名称 """ self.start_app_cmd = f'adb shell am start -W {package_name}/{activity_name}' self.stop_app_cmd = f'adb shell am force-stop {package_name}' def start_cmd(self,cmd): """ 执行命令 :param cmd: :return: """ proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) return proc.stdout.readlines() def start_app(self): """ 启动app :return: """ lines = self.start_cmd(self.start_app_cmd) for line in lines: # bytes -> str result = line.decode(encoding='utf8') if "ThisTime" in result: # 对字符串进行分割处理提取出启动时间 appstart_time = result.strip().split(":")[-1].strip() return appstart_time def stop_app(self): """ 执行关闭app命令 :return: """ self.start_cmd(self.stop_app_cmd) if __name__ == '__main__': app = AppStart('org.cnodejs.android.md','.ui.activity.LaunchActivity') for i in range(10): app.stop_app() result = app.start_app() print(result)