Programmatically close all running apps via ADB
by Tomasz · Published 20th November 2017 · Updated 12th May 2020
It seems and is the most obvious choice to close apps running in background in order to give a device a small boost. Android’s recents screen is designed to allow users to quickly stop apps from running with a simple swipe gesture. But there has to be a way this can be achieved with a script? Given diversity of a designs for recents screen across multitude of manufactures, it would be almost impossible to come up with a solution to automate that gesture, instead there is more basic solution.
1. Close any package
First step is finding a way of closing/killing an app:
As it turns out, its rather simple – activity manager to the rescue. The only required parameter is the package name (e.g com.xanh.draftnotes).
2. Get the list of running apps
Now, we need to get a list of recently opened apps, which after a little bit of digging, turns out not to be as difficult as well:
This might not look like a final solution, however Window manager actually contains a list of currently opened apps (with some additions, that can be easily filtered). Let’s apply some grep to make it more usable.
This should result in following list:
The last step is loop those packages through am force-stop
As a side effect not only running apps will be closed, but also systemUI will restart (device’s screen might go blank for few seconds) and all opened popups (incl. system ones) will be gone.
Источник