是否可以使用 ADB 杀死任务管理器中的ALL活动任务/应用程序?这相当于打开并逐个杀死每个任务...
我尝试使用以下 adb shell 命令,但这并没有终止所有任务。
adb shell am kill-all
我无法使用
adb shell am force-stop <PACKAGE>
命令,因为它需要我知道哪个包/应用程序正在运行。我想杀死所有正在运行的用户应用程序任务。类似于使用任务管理器并一项一项地终止每个任务。
根据命令描述,kill-all杀死所有后台进程。后台进程相当于“服务”,任务相当于“活动”吗?
此外,是否可以使用 ADB 清除应用程序的缓存,同时保留用户数据?我似乎
adb shell pm clear
清除了所有用户数据。我只想清除缓存。
我之所以问这个问题是因为我正在对少数用户应用程序进行一些性能测试。为了使每个测试有效,我想确保所有用户应用程序都没有任何任务、活动、服务和缓存已在后台。
您可以使用
force-stop
,它不需要root权限。
adb shell am force-stop <PACKAGE>
并且可以从最上面运行的activity/app获取包名
adb shell "dumpsys activity | grep top-activity"
之后,您需要对结果进行一些操作,以提取包,这是我的 java 代码:
public void parseResult(String line){
int i = line.indexOf(" 0 ");
if(i == -1){
return;
}
line = line.substring(i);
i = line.indexOf(":");
if(i == -1){
return;
}
line = line.substring(i + 1);
i = line.indexOf("/");
return line.substring(0, i);
}
如果您想开始清理,即关闭应用程序并清除其数据,您可以执行以下操作
adb shell pm clear com.yourapp.package
对于非 root 设备,我扩展了 Faisal Ameer 的脚本
adb shell ps | grep -v root | grep -v system | grep -v "android.process." | grep -v radio | grep -v "com.google.process." | grep -v "com.lge." | grep -v shell | grep -v NAME | awk '{print $NF}' | tr '\r' ' ' | xargs adb shell am force-stop
adb shell am force-stop
不需要root权限。请注意,应用程序仍然显示在运行应用程序抽屉的设备中,但我已验证已使用清除包进程。
adb shell dumpsys meminfo relevant.package.names
查找正在运行的应用程序,忽略系统应用程序等并杀死,以下命令即可完成所有操作;
adb shell ps|grep -v root|grep -v system|grep -v NAME|grep -v shell|grep -v smartcard|grep -v androidshmservice|grep -v bluetooth|grep -v radio|grep -v nfc|grep -v "com.android."|grep -v "android.process."|grep -v "com.google.android."|grep -v "com.sec.android."|grep -v "com.google.process."|grep -v "com.samsung.android."|grep -v "com.smlds" |awk '{print $2}'| xargs adb shell kill
如果您发现任何类似的情况,您可以添加更多例外情况; grep -v“异常”
所有答案都提出了一种停止/强制停止应用程序而不是删除任务的方法,这是另一回事。要删除任务,您首先需要获取最近堆栈中的任务列表。您可以通过使用 dumpsys 和以下命令来获取列表:
dumpsys activity recents
。然后要删除任务,您需要使用 am stack remove {taskId}
和任务 ID,该 ID 在第一个命令的输出中反映为 stackId
。
要将其放在一起,您可以在 adb shell 中使用以下命令来删除最近列表中的所有任务:(我不是 grep/sed 专家,应该有更好的方法来处理输出文本)
dumpsys activity recents | grep -oE "stackId=[0-9]+" | sed 's/stackId=//' | tr '\n' '\0' | xargs -0 -n1 am stack remove
我编写了 shell 脚本,它将关闭所有最近使用的应用程序。
#!/bin/bash
# When this is executed:
# ```
# adb shell dumpsys activity recents | grep 'Recent #0'
# ```
#
# The output will be like this:
# ```
# * Recent #0: Task{46c88ca #115 type=standard A=10162:com.lifemd.care.stage}
# * Recent #1: Task{cb4f5ae #111 type=home I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity}
# ```
#
# The output is filtered to get only `standard` type tasks and then their IDs are extracted.
# The IDs are then used to close the recent apps.
close_all_recent_apps() {
# List all standard tasks in the recent apps
local tasks
tasks=$(adb shell dumpsys activity recents | grep "Recent #" | grep "type=standard")
# Split the output into an array
IFS=$'\n' read -rd '' -a tasks <<<"$tasks"
# Close each recent app
for task in "${tasks[@]}"; do
local task_id
task_id=$(echo "$task" | sed -n 's/.*#\([0-9]*\).*/\1/p')
echo "Closing the recent app with ID: $task_id"
adb shell am stack remove "$task_id"
done
}
close_all_recent_apps