Create rooted android shell script that will force stop all user apps except app in…
- User
Create rooted android shell script that will force stop all user apps except app in recent item/overview button
- Assistant
Create rooted android shell script that will force stop all user apps except app in recent item/overview button
Creating a shell script to force stop all user-installed apps except the one currently in the recent apps (overview) requires careful handling. This script assumes that your device is rooted, which is necessary for executing certain commands with elevated privileges.
Below is a step-by-step guide along with the complete script to achieve this functionality.
Retrieve the Top Recent App:
dumpsys activity recents to get the list of recent tasks.List All User-Installed Apps:
pm list packages -3 to list third-party (user-installed) apps.Exclude the Top Recent App:
Force Stop Remaining Apps:
am force-stop.#!/system/bin/sh
# Ensure the script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root. Exiting."
exit 1
fi
# Function to get the top recent app's package name
get_top_recent_app() {
# Dump recent tasks
RECENT_TASKS=$(dumpsys activity recents)
# Extract the first TASK_RECORD
FIRST_TASK=$(echo "$RECENT_TASKS" | awk '/TASK_RECORD/{flag=1; print; exit}')
if [ -z "$FIRST_TASK" ]; then
echo "No recent tasks found."
exit 1
fi
# Extract package name using regex
# Example line: mActivityComponent: ComponentInfo{com.example.app/.MainActivity}
PACKAGE_LINE=$(echo "$FIRST_TASK" | grep -o "ComponentInfo{[^}]*}")
if [ -z "$PACKAGE_LINE" ]; then
echo "Could not parse package name from recent tasks."
exit 1
fi
# Extract package name from ComponentInfo
# ComponentInfo{com.example.app/.MainActivity} -> com.example.app
PACKAGE_NAME=$(echo "$PACKAGE_LINE" | sed -n 's/ComponentInfo{\([^/]*\)\/.*/\1/p')
echo "$PACKAGE_NAME"
}
# Get the top recent app package name
TOP_APP=$(get_top_recent_app)
if [ -z "$TOP_APP" ]; then
echo "Failed to retrieve the top recent app."
exit 1
fi
echo "Top recent app: $TOP_APP"
# Get list of all user-installed (third-party) apps
USER_APPS=$(pm list packages -3 | sed 's/package://')
if [ -z "$USER_APPS" ]; then
echo "No user-installed apps found."
exit 1
fi
echo "Total user-installed apps found: $(echo "$USER_APPS" | wc -l)"
# Exclude the top recent app from the list
APPS_TO_STOP=$(echo "$USER_APPS" | grep -v "^$TOP_APP$")
if [ -z "$APPS_TO_STOP" ]; then
echo "No apps to stop after excluding the top recent app."
exit 0
fi
echo "Apps to be force stopped: $(echo "$APPS_TO_STOP" | wc -l)"
# Iterate and force stop each app
for PACKAGE in $APPS_TO_STOP; do
echo "Force stopping: $PACKAGE"
am force-stop "$PACKAGE"
# Optional: Add a short delay to prevent overloading the system
sleep 0.1
done
echo "Force stop operation completed."
exit 0
Create the Script File:
force_stop_apps.sh.Paste the Script:
Save and Exit:
Transfer to Your Device:
/data/local/tmp/.Set Execute Permissions:
adb shell to access your device's shell.chmod +x force_stop_apps.sh
Execute the Script:
./force_stop_apps.sh
su to switch to root before executing:
su
./force_stop_apps.sh
Root Check:
Retrieving Recent Tasks:
dumpsys activity recents provides details about recent tasks.Listing User Apps:
pm list packages -3 lists all third-party (user-installed) apps. The -3 flag filters out system apps.Excluding the Top App:
grep -v to remove the top recent app from the list of apps to be force stopped.Force Stopping Apps:
am force-stop.sleep 0.1) is included to prevent overwhelming the system with rapid commands.Multiple Recent Apps:
Dynamic Recent Apps:
Automating Execution:
Logging:
This script provides a foundational approach to force stopping user-installed apps while excluding the top recent app in the overview. Always exercise caution when force stopping applications, as it may affect app stability or system performance. Test the script thoroughly to ensure it behaves as expected on your specific device and Android version.
Not yet included in a published release.