python 直接自动执行adb指令,清除所有后台应用。
要不直接上代码吧!
# This is a sample Python script.
import subprocess
import re
def clear_app():
# adb shell dumpsys activity
mTaskRecord = subprocess.run(['adb', 'shell', 'dumpsys', 'activity'], stdout=subprocess.PIPE)
# # 0 ActivityRecord{8017ab3 u0 com.mediatek.filemanager/.FileManagerOperationActivity t15} type=standard mode=fullscreen
# ['com.android.systemui', 'com.android.launcher3',
print(type(mTaskRecord.stdout.decode()))
result = re.findall(r"#0 ActivityRecord{(.+?)/", mTaskRecord.stdout.decode())
packageNameList = []
# 该应用不能清除所以排除在外
list = ['com.android.systemui', 'com.android.launcher3']
for data in result:
if data.split(' ')[2] not in list:
packageNameList.append(data.split(' ')[2])
for pck in packageNameList:
subprocess.run(['adb', 'shell', 'am', 'force-stop', pck], stdout=subprocess.PIPE)
print(packageNameList)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
result = clear_app()