1. Check out our companion app, Forums for Android! Download from Google Play

WiFi control when launching an app

Discussion in 'Android Apps & Games' started by skawka, May 1, 2013.

  1. skawka

    skawka New Member
    Thread Starter

    May 1, 2013
    Hi all,

    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.



  2. PantsDownJedi

    PantsDownJedi Member

    May 30, 2013
    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


    sleep 2
    while [ "$x" != "" ] ; do
    x=`dumpsys activity | grep com.facebook.katana/.LoginActivity`
    sleep 2
    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.
  3. WarriusZero

    WarriusZero Guest

    There should be 2 easier ways to do that.

    The simplest way is to turn the problem around. Why not create a profile that will switch the Wi-Fi on when the app is running:

    Profile: BBC News Running
    Application > BBC News

    Task: Switch Wi-Fi On
    Net > WiFi: on

    Exit Task: Switch Wi-Fi Off
    Net > WiFi: off

    Now I agree that this is not exactly what the OP is asking for. He may want the Wi-Fi to be available before the app is running. So what about:

    Profile: BBC News Running
    Application > BBC News

    Task: BBC News Started
    Variables . Variable Set: %BbcNewsRunning to yes

    Exit Task: BBC News Stopped
    Variables . Variable Set: %BbcNewsRunning to no

    Op task:
    Net > WiFi: on
    Task > Wait: 10 seconds
    App > Load App: BBC News
    Task > Wait Until: 10 seconds, Until %BbcNewsRunning !~ yes
    Net > WiFi: off

Share This Page