Termux: Using Rish ADB with DUMPSYS to determine the running state for a package

descriptionStandard

In this article we cover how to use Termux in combination with Rish to get extended properties about a package such as the last visible package, is a specific package visible; this could be use to trigger additional actions from Tasker when the running state chanages.

Required: If you haven’t already configured Shizuku (Rish) with Termux there is a guide available at: Termux, Shizuku and Rish configuration for Android 14

Rish variants for ADB commands

Note the following table assume that you have installed Rish within Termux at path: /data/data/com.termux/files/usr/bin/rish_auto if you have installed Rish with a different Shell or path ensure you update it here.

In the example below replace: <package-name> with the desired package you are targeting.

DescriptionCommandExample results
Is package in the foreground/background or not runningrish_auto -c "dumpsys activity activities | grep -A 40 'packageName=<package-name>'" | grep nowVisible= | tr -s ' ' | cut -d' ' -f2 | cut -d'=' -f2Returns empty if app is not running.

True if in the foreground.

False if in background.
Remove a system app from the battery optimisation whitelistrish_auto -c "dumpsys deviceidle sys-whitelist -<package-name>"
Remove a system app from the battery optimisation whitelistrish_auto -c "dumpsys deviceidle sys-whitelist +<package-name>"
Remove a user app from battery optimisations whitelistrish_auto -c "dumpsys deviceidle whitelist -<package-name>"
Add a user app to battery optimisations whitelistrish_auto -c "dumpsys deviceidle whitelist +<package-name>"
ADB Dumpsys examples using Rish

These commands open up the possibility to automate a device beyond typical scenarios using Tasker and Termux. For example triggering a task or specific action when a app is in the foreground or background.