Originally Posted by skawka
I'm trying to create a simple procedure using TASKER that basically turns on Wifi, waits a few seconds, then runs an app that requires network access.
My problem is that I also want to turn off Wifi ONLY when the app closes but unfortunately have so far found no way to do so.
TASK: WiFi ON
TASK: WAIT (a few secs)
TASK: LOAD app
TASK: WiFi Off
However, as soon as the app loads and starts running, WiFi is immediately turned off.
Does anyone know of a way to somehow detect that the app is running and prevent the WiFi Off command from executing UNTIL the app closes ?
The reason that I have to manually turn WiFi ON is because some of my apps generate error messages if network access is unavailable when the app starts.
Quite a late reply but I just found this Tasker section and signed up to the forum.
One way to do it is at the shell if you're rooted.
After Load App, you can run a shell script that checks every few seconds to see if the app is still running, and when it isn't it exits. How Android allows this is differant on every phone. I've used shell scripts on the phone that were useful, select scripts and shell, then without the quotes "sh /path/to/the/script" and with this failed I used sl4a to run it, however this also doesn't work well sometimes in which case everything has to go into a single line at script->run shell with semicolons separating each command. If you're familiar with sh or bash (sh has more limitations but is for the most part compatible), then you would have a starting point for it. If you're good at some other language and sl4a runs on your phone then that's just as well.
In any case, it takes a lot of poking around to find exactly what you need, for example you don't want it to close when its backgrounded, but to get you started, this command run at the shell using adb will tell you what the forground process is
[HIGH]dumpsys activity top | grep '^TASK.*' | cut -d ' ' -f2[/HIGH]I got that from here http://forum.xda-developers.com/showpost.php?p=30175170&postcount=3
under the section "Force stopping an application" Its a snippet of the command t0 force stop whats in the foreground window. Its the part that shows what's in the foreground window.
Note that not everything on that page applies globaly to Android and some of the info on the page is only covered under ICS and possibly only the SGS2 but this command has remained the same for me across 3 upgrades and 3 different phones and on AOSP ROMs as well.
From there you can use dumpsys and direct into a file while your app is forgrounded, backgrounded, or closed or running as a service and get a grasp on what exactly you need to look for, then set up a shell script that looks something like this, which I did for Facebook
while [ "$x" != "" ] ; do
x=`dumpsys activity | grep com.facebook.katana/.LoginActivity`
done[/HIGH]If you can't call the script from tasker, it looks a little differant behind do and then statements when you set it up as a one liner in tasker, so the above would look like this
[HIGH]sleep2 ; x="something" ; while [ "$x" != "" ] ; do x=`dumpsys activity | grep com.facebook.katana/.LoginActivity` ; sleep 2 ; done[/HIGH]Note that the end of line after do in the script implies a semicolon but that it isn't used when running it all as a one liner. Its the same if you use if [ ] ; then .... you wouldn't put a semicolon after then.
The initial sleep 2 at the beginning is optional as you're already using Wait in Tasker to achieve the same thing.
If you tick continue after error, this will not work as the task will not hang until the loop in the shell exits, but move on to the next step in the task. You do want to have use root ticked though.
There may well be a much easier way than this that's built into the Tasker app, but this is how I've been doing this sort of thing and haven't found any other answers.
Good luck. I hope to find a better answer at some point.
As an afterthought, I've never tried it, but if there's a way to set up a state condition for when an app runs, you can probably have enter and exit tasks that toggle the wifi... I'm guessing that the lack of prior reply suggests this isn't possible though. It would be a hit on your battery unless you could toggle that state profile in conjunction with the task... in any case I doubt its possible. The thins I've set up are for unfreezing an app that usually runs as a service, running it, then freezing it down again on exit, so this is where my example at the shell comes in, in relation to your problem.