Programmatically close all running apps via ADB
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:
am force-stop: force stop everything associated with <PACKAGE>. --user <USER_ID> | all | current: Specify user to force stop; all users if not specified.
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:
$ adb shell dumpsys window a WINDOW MANAGER ANIMATOR STATE (dumpsys window animator) DisplayContentsAnimator #0: Window #0: WindowStateAnimator{28c00a1 com.android.systemui.ImageWallpaper} Window #1: WindowStateAnimator{dd8ddb0 com.xanh.draftnotes.debug/com.xanh.draftnotes.MainActivity} Window #2: WindowStateAnimator{d744504 com.google.android.youtube/com.google.android.apps.youtube.app.WatchWhileActivity} Window #3: WindowStateAnimator{ca0caed com.google.android.apps.plus/com.google.android.apps.plus.phone.BinderHomeActivity} Window #4: WindowStateAnimator{b5382c6 com.android.systemui/com.android.systemui.recents.RecentsActivity} Window #5: WindowStateAnimator{ab7acf3 com.google.android.googlequicksearchbox/com.google.android.launcher.GEL} Window #6: WindowStateAnimator{f023429 InputMethod} Window #7: WindowStateAnimator{7c705dd AssistPreviewPanel} Window #8: WindowStateAnimator{9750552 KeyguardScrim} Window #9: WindowStateAnimator{dde7023 StatusBar} Window #10: WindowStateAnimator{bf29920 NavigationBar} no ScreenRotationAnimation mAnimTransactionSequence=19515 mForceHiding=KEYGUARD_NOT_SHOWN mCurrentTime=360543020 (1553036 ms ago) mBulkUpdateParams=0x8 ORIENTATION_CHANGE_COMPLETE
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.
$ adb shell dumpsys window a | grep "/" | cut -d "{" -f2 | cut -d "/" -f1 | cut -d " " -f2
This should result in following list:
com.xanh.draftnotes.debug com.google.android.youtube com.google.android.apps.plus com.android.systemui com.google.android.googlequicksearchbox
The last step is loop those packages through am force-stop <package>:
APPS=$(adb -s $DEVICE shell dumpsys window a | grep "/" | cut -d "{" -f2 | cut -d "/" -f1 | cut -d " " -f2) for APP in $APPS ; do echo "Closing: $APP" adb shell am force-stop $APP done
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.