Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I kill all active tasks/apps using ADB?

Is it possible to kill ALL the active tasks/apps in the task manager using ADB? This would be equivalent of opening the task manager and killing each task one by one...

I tried using the the following adb shell command but that didn't kill all the task.

adb shell am kill-all

I can't use the adb shell am force-stop <PACKAGE> command because it would require me to know which package/app is running. I want to kill ALL the user apps task that are running. Similarly to using the task manager and killing each task one by one.

According to the command description, kill-all kills all background processes. Are background processes equivalent to "services" and task equivalent to "activities"?

Also, is it possible to clear cache of apps using ADB while keeping the user data? I seems that the adb shell pm clear clears all the user data. I want to only clear the cache.

The reason why I am asking is because I am doing some performance testing on few user apps. To make each test valid, I want to ensure none of the user apps have any task, activities, services, and cache already in the background.

like image 623
Zythyr Avatar asked Jun 26 '15 18:06

Zythyr


People also ask

How do I force quit an app using adb?

In Devices view you will find all running processes. Choose the process and click on Stop . It will kill only background process of an application. adb shell am kill [options] <PACKAGE> => Kill all processes associated with (the app's package name).


1 Answers

You can use force-stop, it doesn't require root permission.

adb shell am force-stop <PACKAGE>

And you can get the package name from the top running activity/app

adb shell "dumpsys activity | grep top-activity"

After that you need to play a bit with the result, to extract the package, here my java code that does that:

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);
}
like image 180
Ilya Gazman Avatar answered Sep 22 '22 09:09

Ilya Gazman