Check if app is running?

3,493 views
Skip to first unread message

leonardo.be...@gmail.com

unread,
Sep 14, 2014, 12:53:13 PM9/14/14
to automa...@googlegroups.com
I want to start the GPS when maps is activated. I have tried the block "App in forgrund" but I don't get it to work. In the log it either just wait in that block or repeats it depending if I put exact or Immediately or When changed as option.

Automate developer

unread,
Sep 14, 2014, 3:02:00 PM9/14/14
to automa...@googlegroups.com, leonardo.be...@gmail.com
You only use "Immediately" when you want to check the current state, without waiting for it to change.
This should work:
                  :
+-----------------+
|                 |
|      When App in foreground -----------------------+
|           When changed                             |
|  Package: com.google.android.apps.maps             |
|                 |                                  |
|     Location provider set state       
Location provider set state
|              GPS on                             GPS off
|                 |                                  |
+-----------------+----------------------------------+

Viggo Jorgen

unread,
Sep 17, 2014, 6:39:42 PM9/17/14
to automa...@googlegroups.com
One thing regarding app in foreground I noticed recently - sometimes when launching apps, Automate doesn't detect foreground app change properly, it happened with screen timeout for me, so I'm not sure it is really issue with foreground apps detection or with timeout itself

Automate developer

unread,
Sep 17, 2014, 7:07:35 PM9/17/14
to automa...@googlegroups.com
The only way of checking for foreground app, is polling the "activity stack". And Google really don't like the app developers using the technique:
http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks%28int%29

There's no documentation on what's happens to the "activity stack" when the screen timeouts.
But my guess is that the timeout start an activity, like the lock screen app, and that becomes the foreground.

Viggo Jorgen

unread,
Sep 18, 2014, 3:57:31 AM9/18/14
to automa...@googlegroups.com
I was unclear - let me explain it one more time.
I've got flow which changes screen timeout to 1min when in forgeround is any app but launcher, when launcher is in foreground screen timeout is set to 10s.

Now for launching apps I use App Swap - it is sort of drawer which replaces Google Now "swipe form home" gesture. Sometimes when I'm on home screen and launch app fom AS Automate doesn't notice foreground app change, screen timeout stays at 10s and stays at 10s no matter what I do later, only way to fix this is going back to home screen and launch app again, so Automate detects it properly.

However, this never happens when launching app directly from launcher...

Automate developer

unread,
Sep 18, 2014, 3:03:25 PM9/18/14
to automa...@googlegroups.com
Make a simple flow that just displays the current, most foreground app:
             :
+------------+
|            |
|     App in foreground
|       When changed
|     Args: both empty
|     Vars: pkg, cls
|            |
|      Display message
|    Message: pkg++" "++cls
|            |
+------------+

Run it, and see what happens. Does the foreground app change?

The "Google now swipe" is called an assist request, Automate can also handle it using the Assist request block.
It may be that, when an app is launched from an assist request, it's not going to the top of the "activity stack",
but instead, just top of the app requesting assist.

Viggo Jorgen

unread,
Sep 25, 2014, 5:53:42 PM9/25/14
to automa...@googlegroups.com
Well, I created this flow but it seems that nothing ever is displayed and flow continuously waits for foreground app to change. Timeout changes though, but in 90% cases, the other 10% Automate can't detect foreground app properly.

Mark Newton

unread,
Sep 25, 2014, 7:14:15 PM9/25/14
to
What launcher are you using? I just made a flow that does what you described and its working perfectly with App Swap. The only problem I ran into, was that if you are using the Google Now Launcher, you have to pick Google Search as the launcher app. Otherwise it hangs on the "When foreground app" waiting for a state change. Also, you dont have to pick a class receiver, just select the launcher app in the package section. Leave the class receiver blank. If you have to delete a class receiver, click the function button next to it, delete the text, then save the block. I can share the flow I made if you want.

Viggo Jorgen

unread,
Sep 25, 2014, 7:18:10 PM9/25/14
to automa...@googlegroups.com
I use Nova, on stock 4.4
Sure, share flow, I'll check it

Mark Newton

unread,
Sep 25, 2014, 7:33:53 PM9/25/14
to automa...@googlegroups.com
https://www.mediafire.com/?5bo15aakbi0o0ub

Remember to change the package to your launcher. Let me know if it works.

Mark Newton

unread,
Sep 25, 2014, 8:28:53 PM9/25/14
to automa...@googlegroups.com
You can remove the log appends from the flow if everything is working properly. Just make sure to put the correct connections in place.

Viggo Jorgen

unread,
Sep 26, 2014, 5:12:13 AM9/26/14
to automa...@googlegroups.com
It's bassicaly the same flow as mine, except logging part, and without initial immediately app chceck block. Do you think lack of it in my flow can cause problems?

Viggo Jorgen

unread,
Sep 26, 2014, 10:50:25 AM9/26/14
to automa...@googlegroups.com
2014-09-26 16:43:10.314 INFO(363): Screen off timeout set
2014-09-26 16:43:10.356 USER(363): Timeout 15s
2014-09-26 16:43:10.365 INFO(363): App in foreground?
2014-09-26 16:43:13.635 INFO(363): Screen off timeout set
2014-09-26 16:43:13.663 USER(363): Timeout 1m
2014-09-26 16:43:13.665 INFO(363): App in foreground?
2014-09-26 16:43:32.487 INFO(363): Screen off timeout set
2014-09-26 16:43:32.529 USER(363): Timeout 15s
2014-09-26 16:43:32.547 INFO(363): App in foreground?
2014-09-26 16:44:41.189 INFO(363): Screen off timeout set
2014-09-26 16:44:41.204 USER(363): Timeout 15s
2014-09-26 16:44:41.238 INFO(363): App in foreground?

Two last entries are from two different apps, Automate simply haven't noticed change. Only way to make it aware of foreground app is back to launcher, then launch other app again. So lack of first block isn't issue here...

Mark Newton

unread,
Sep 26, 2014, 6:05:06 PM9/26/14
to
Ive noticed the same behavior actually. I tried to put as much information into the log as possible. This is a log using App Swap, but I'm going to try without using it and see if I get the same result.

2014-09-26 15:44:11.384 INFO(645): App in foreground?
2014-09-26 15:44:13.431 USER(645): Foreground App 1m:com.llamalab.automate
2014-09-26 15:44:13.434 INFO(645): Screen off timeout set
2014-09-26 15:44:13.443 INFO(645): Display message
2014-09-26 15:44:13.456 USER(645): Timeout 1m
2014-09-26 15:44:13.457 INFO(645): App in foreground?
2014-09-26 15:44:17.291 USER(645): Foreground App 15s:com.google.android.googlequicksearchbox
2014-09-26 15:44:17.367 INFO(645): Screen off timeout set
2014-09-26 15:44:17.395 INFO(645): Display message
2014-09-26 15:44:17.398 USER(645): Timeout 15s
2014-09-26 15:44:17.400 INFO(645): App in foreground?
2014-09-26 15:44:20.691 USER(645): Foreground App 15s:com.google.android.googlequicksearchbox
2014-09-26 15:44:20.697 INFO(645): Screen off timeout set
2014-09-26 15:44:20.699 INFO(645): Display message
2014-09-26 15:44:20.708 USER(645): Timeout 15s
2014-09-26 15:44:20.723 INFO(645): App in foreground?
2014-09-26 15:44:22.987 USER(645): Foreground App 1m:com.llamalab.automate
2014-09-26 15:44:22.988 INFO(645): Screen off timeout set
2014-09-26 15:44:22.997 INFO(645): Display message
2014-09-26 15:44:23.007 USER(645): Timeout 1m

The second com.google.android.googlequicksearchbox is me launching chrome. For some reason automate detected the launcher instead. This happened once before when launching the clock app as well.

Automate developer

unread,
Sep 26, 2014, 6:11:48 PM9/26/14
to automa...@googlegroups.com
Thanks for the details. I'll do some bug hunting.


On Saturday, September 27, 2014 12:05:06 AM UTC+2, Mark Newton wrote:
Ive noticed the same behavior actually. I tried to put as much information into the log as possible. This is a log using App Swap, but I'm going to try without using it and see if I get the same result.

2014-09-26 15:44:11.384 INFO(645): App in foreground?
2014-09-26 15:44:13.431 USER(645): Foreground App 1m:com.llamalab.automate
2014-09-26 15:44:13.434 INFO(645): Screen off timeout set
2014-09-26 15:44:13.443 INFO(645): Display message
2014-09-26 15:44:13.456 USER(645): Timeout 1m
2014-09-26 15:44:13.457 INFO(645): App in foreground?
2014-09-26 15:44:17.291 USER(645): Foreground App 15s:com.google.android.googlequicksearchbox
2014-09-26 15:44:17.367 INFO(645): Screen off timeout set
2014-09-26 15:44:17.395 INFO(645): Display message
2014-09-26 15:44:17.398 USER(645): Timeout 15s
2014-09-26 15:44:17.400 INFO(645): App in foreground?
2014-09-26 15:44:20.691 USER(645): Foreground App 15s:com.google.android.googlequicksearchbox
2014-09-26 15:44:20.697 INFO(645): Screen off timeout set
2014-09-26 15:44:20.699 INFO(645): Display message
2014-09-26 15:44:20.708 USER(645): Timeout 15s
2014-09-26 15:44:20.723 INFO(645): App in foreground?
2014-09-26 15:44:22.987 USER(645): Foreground App 1m:com.llamalab.automate
2014-09-26 15:44:22.988 INFO(645): Screen off timeout set
2014-09-26 15:44:22.997 INFO(645): Display message
2014-09-26 15:44:23.007 USER(645): Timeout 1mEnter code here...

quip...@gmail.com

unread,
Oct 5, 2014, 12:55:00 PM10/5/14
to automa...@googlegroups.com
In addition to polling the activities you could use an AccessibilityService to receive callbacks when the window content/state changes. You can extract the package name from the accessibility event callback to determine whether an app was opened or closed. I'm using this approach successfully in AppTracker, and it seems to be easier on the battery. Of course, that does require another service running...

Automate developer

unread,
Oct 5, 2014, 2:25:42 PM10/5/14
to automa...@googlegroups.com, quip...@gmail.com
Thanks for the tip.

I've just added the accessibility service. I know there's a lot you can do with it, so more cool blocks are coming.
Using instead of polling sound like a better option. I'll implement it.

rodrigo.ol...@gmail.com

unread,
Apr 14, 2015, 2:44:27 PM4/14/15
to automa...@googlegroups.com
Hi guys, I hope you can help me , maybe I'm not that good in automate but still I'm not finding any way to see if a specific app is running, not only in foreground, or if that app was closed or killed.

The thing is that I'm trying to enable various settings when a specific app is running and disable them when the app is terminated, not when it is in foreground or not , just running or not running.

Is there a way to know??

Thanks...

Automate developer

unread,
Apr 14, 2015, 7:07:59 PM4/14/15
to automa...@googlegroups.com, rodrigo.ol...@gmail.com
That's not really how Android works. An app can be in the background with or without having its process running.
A proper Android app that's in the background is paused, and can't/shouldn't be doing anything, unless it's using a Service.

Currently Automate has no block for checking if a service or an app process is running.
You may be able to use the Shell command block with the ps command to check if a process is running.
Reply all
Reply to author
Forward
0 new messages