r/tasker • • Oct 30 '26

What is the most straightforward, efficient way for Tasker to test whether a given app is running and then bring it to the foreground?

I'm using the latest Tasker version 6.7.7-rc under Android 13 that's rooted with the latest APatch.

In certain special cases, I would like to implement the following logic within a Tasker task. This, of course, is just pseudo-code, and I use the term "certain-app" to refer to any given app ...

if ( certain-app is currently running )
then
  if ( certain-app is not the current foreground app )
  then
    cause certain-app to become the current foreground app
  endif
endif

I know there is more than one way to do this in Tasker, but I'm looking for the most straightforward way to accomplish this with the very least amount of code that needs to be written in order to implement this logic in a Tasker task.

Thank you very much in advance for thoughts and suggestions.

0 Upvotes

10 comments sorted by

3

u/DifficultyCrafty7623 Oct 30 '26
Task: Bring Tasker to Foreground

A1: Variable Set [
     Name: %app_target
     To: net.dinglisch.android.taskerm
     Structure Output (JSON, etc): On ]

A2: App Info [ ]

A3: Launch App [
     Package/App Name: %app_target ]
    If  [ %app_package neq %app_target ]

Replace Tasker with your app package

1

u/unselective-amnesia Oct 30 '26 edited Oct 30 '26

Thank you very much!

Also, a general, related question:

How can I determine in a Tasker task whether or not any given app is running at all?

Or is it best to do this with a shell script that gets called by Tasker?

5

u/Exciting-Compote5680 Oct 30 '26 edited Oct 30 '26

I was trying to get something similar working a while back. I'm a bit fuzzy on the details, but I found this task:

Task: UT Is App Active

<Requires ADB WiFi (Wireless debugging enabled), root and/or Shizuku>
A1: Anchor

A2: Multiple Variables Set [
     Names: %app=%par1
     %is_active=false
     %pid=null
     Variable Names Splitter: 
     
     Values Splitter: = ]

<Manual input>
A3: Variable Set [
     Name: %app
     To: com.spotify.music
     Structure Output (JSON, etc): On ]
    If  [ %app ~R ^% ]

A4: App Info [
     Package/App Name: %app ]

<Get process id>
A5: ADB Wifi [
     Command: pidof %app_package
     Timeout (Seconds): 10 ]

A6: Multiple Variables Set [
     Names: %is_active=true
     %pid=%aw_output
     Variable Names Splitter: 
     
     Values Splitter: = ]
    If  [ %aw_output > 0 ]

A7: Flash [
     Text: App: %app_name
     Is_active: %is_active
     Pid: %pid
     Long: On
     Tasker Layout: On
     Continue Task Immediately: On
     Dismiss On Click: On ]
    If  [ %caller(1) ~ ui ]

A8: Return [
     Value: %is_active
     Stop: On ]


It didn't quite work for my use case. I was trying to determine if Spotify was active, or needed to be brought to the foreground in order to execute media commands. I think the problem was that Spotify would go to sleep without the pid being cleared or something like that. But perhaps it works for you/your use case. The 'interesting' part is A5 obviously.

1

u/unselective-amnesia Oct 30 '26

Thank you very much! I'll give this a try.

3

u/unselective-amnesia Oct 30 '26

Actually, I just realized that the following command works from the shell:

/system/bin/pgrep com.textra # or any other package name

It either writes the pid of the active process to stdout, or if there is no such process, it returns nothing to stdout.

I can easily run this via the "Run Shell" action, passing it the package name of the app I'm interested in, and then checking the output within the Tasker task.

3

u/Exciting-Compote5680 Oct 30 '26

Hope it helps. If you end up finding a good way to detect if an app is running in the background, feel free to share. The result of my attempt was not quite satisfactory. 

2

u/unselective-amnesia Oct 30 '26

Yes, determining whether an app is in the foreground or background is more complicated than simply checking whether or not its process is running.

I'll keep plugging away at this issue in my spare time, and I'll report whatever I might figure out.

3

u/Exciting-Compote5680 Oct 30 '26

I came across this post: 

https://www.reddit.com/r/tasker/comments/14hbq68/looking_for_task_to_kill_all_running_background/

By the looks of it, a lot of interesting info and links in there. 

2

u/unselective-amnesia Oct 30 '26

Indeed lots of useful info. Thank you!

Regarding the question of whether an app is running in the foreground, the background, or not at all, I think that the following procedure might work (although I don't have time to test it today). In this procedure, "app-pkg-name" is the app's package name:

* If /system/bin/pgrep app-pkg-name returns no data, then the app isn't even running.

* If the app is running and %app_package is equal to app-pkg-name (after running "App Info"), then the app is in the foreground.

* If neither is true, then the app is running in the background.

I'll test this within the next day or two, and I'll report my findings.