Thanks,-Dustin
Unfortunately pgrep does not differ between running and (inactive) cached processes. Do you know how to distinguish?I do not want to turn off GPS for sport tracking app when it is running in background, but when app is in cache it is ok to turn off.Maybe something similar to pgrep (for processes) but for services exists?
On Sunday, 15 July 2012 22:53:28 UTC+2, Tim wrote:I'm new to Android but I wanted to achieve a similar function and I figured out how to do it - after some googling... so I figured I'd share.
I installed "Secure Settings" and by using the plugin in Tasker I set the pid of the application (pgrep <appname>) to a variable I can then "Test" against it in Tasker on both Enter and Exit to control what gets turned on or off.
| me (Dave White change) | Post reply![]() |
The command name for ps looks to be the last 15 characters of the package name.
I've only looked at 2 apps, but for oom_adj, when moved away from foreground, one was 2 and the other 10. Both were 9 after actually quitting the app.
Rick
Endo GPS (8)
A1: Variable Clear [ Name:%ENDO_CAT Pattern Matching:Off ]
A2: Secure Settings [ Configuration:cat Package:com.intangibleobject.securesettings.plugin Name:Secure Settings ]
A3: Wait Until [ MS:1 Seconds:0 Minutes:0 Hours:0 Days:0 ] If [ %ENDO_CAT Is Set ]
A4: If [ %ENDO_CAT < 3 ]
A5: Flash [ Text:GPS enabled Long:Off ]
A6: GPS [ Set:On ]
A7: Else
A8: Flash [ Text:GPS disabled Long:Off ]
A9: GPS [ Set:Off ]
A10: End If
exit task actions
stop if %GPS matches off [because if already off, nothing to do]
run shell > ps YourNavAppPkg , set var %res to output
parse %res for pid, store in %pid
run shell > cat /proc/%pid/oom_adj , set var %oomadj to output
stop if %oomadj > 6 [threshhold for background app? need to tweak? ]
open dialog settings > gps & network location
stop
Tom
Rick hashed out the nitty gritty details, look through this thread for his posts on how he did it.
Tom
You might try a cooldown time of a minute or so, that should prevent the endless looping you described.
Tom
But to summarize here:
To detect when an app in in background, run that app (like Google Navigation, with a destination) and then let it go to to background ( like by opening a terminal emulator app).
Run the shell command "ps" and look through the displayed processes to find that app's process. Note that an app's name may differ from the package name. From the ps listing, find the process id (PID).
Run the command "cat /proc/PID/oom_adj", replacing PID with the actual process id. in return, you'll get back a number, ranging from as low as -17 to as high as 17. An app running in the Foreground tend to have an oom_adj score of 1 or 0 or maybe even lower. An app in Background may have a score of 2 or 3 or higher.
The shell commands ps and cat should be available on stock unrooted phones.
Tom
getprop | grep "_APP_ADJ"
Tom
Hello,I need to have my LTE turned off while i'm on my IRC app at work. My building get low lte signal and sometimes it will switch to LTE. it will disconnect me form the IRC server every time.I've tried to have the context @ work (location) and Application -AndroIRC. Since my Rom doesn't support LTE toggle i just have it open the mobile data settings on the enter task and the exit task when i'm done using AndroIRC. the problem is it only recognizes the app while it is opened and not that its running in the background and on the ongoing notifications. basically every time i open the app it will pull up the settings screen and also when i close the settings it tries to open it back up because I've returned AndroIRC what kind of work around is there. or is there a way to have Tasker recognize when com.androirc is running? I've been looking in the guide haven't found anything use to my situation.Thanks,-Dustin
Tom
> There's one dev (Pent). I don't know if fore/background process detection is on his todo list. The todo list is massive, by the way.
Yep.. It's there.
Item number 16,531---- running background service detection
Just kidding about the item number :) But it is a rather large list. The list is on the tasker main website Under "Plans"
Rich..
Wow, great work.. This will be very helpful ..
Rich..
As a result, it return "0"
Do you guess why?
Some great info on here - thanks all.I've just used this to implement a profile change when I'm in my car app (ulysses speedo). Pretty much cloned Harald's task and assigned it to the entry/exit task for a profile which checks for the app.Its a bit messy, as it tends to try and set the car profile a few times when I flip between the speedo and nav/music apps, but this could be easily fixed with a variable.
Ap (44)
A1: Perform Task [ Name:Ras Stop:Off Priority:5 Parameter 1 (%par1):com.skype.raider Parameter 2 (%par2): Return Value Variable:%APPRUN ]
A2: Wait Until [ MS:250 Seconds:0 Minutes:0 Hours:0 Days:0 ] If [ %APPRUN Is Set ]
A3: Flash [ Text:%APPRUN Long:Off ]
try create this but don't work
ps
You should see a flood of processes. Let's narrow it down to just the line we want:
ps | grep -i MyMusic
Hopefully that works and produces one row. You can also try this command:
pgrep MyMusic
to return the process id (pid) of MyMusic. If that worked, (let's say the pid was 1234), you can type this:
cat /proc/1234/oom_adj
That should return a number. If that worked, you can do all of the above in one step in the shell like this:
cat /proc/$(pgrep MyMusic)/oom_adj
If that works, then you can put that into Tasker's run shell action like this:
RunShell > Command:
cat /proc/$(pgrep MyMusic)/oom_adj
Store Output in Variable: %ShellOutput
The returning answer will get stored in the var %ShellOutput
I hope that helps.
Tom
Let's say you have an Task defined called DoubleIt that would calculate for you the sum of a number with itself. If you wanted to run DoubleIt on the number 5, you can call it with the action:
Task > PerformTask > Task: DoubleIt
Parameter1: 5
Return: %TheAnswer
Alert > Notify > Title: double of 5 is %TheAnswer
The Task DoubleIt would look like this:
set Variable > name: %doubled
value: %par1 * 2
do maths: Yes
task > return > value: %doubled
Tom
Output: hello there (the output of the shell command)
Result: 0 (whether or not the command succeeded. 0 means success for result codes)
Error: (nothing, because no error)
Tom
ps | grep speedo
or
pgrep speedo
Anything?
Tom
ps | grep -i speedo
Just to be clear, that's:
ps [space] [vertical bar] [space] grep [space] [minus] i [space] speedo
Tom
Tom
Here's what I get when I enter what you suggested a couple messages back.
$ ps | grep -i speedo
grep: not found
$
JL
Cut and paste for me the exact command(s) you're typing into the shell, and the exact output or errors you're getting back.
Tom
Matt
AppRunning (49)
A1: Run Shell [ Command:cat /proc/$(pgrep %par1$)/oom_adj Timeout (Seconds):0 Use Root:Off Store Output In: Store Errors In: Store Result In:%oomadj ]
A2: Flash [ Text:Oom_adj for %par1 is %oomadj Long:Off ]
A3: Return [ Value:1 Stop:On ] If [ %oomadj ~ 0/2 ]
A4: Return [ Value:0 Stop:Off ]
DetectApp (51)
A1: Perform Task [ Name:AppRunning Stop:Off Priority:5 Parameter 1 (%par1):com.android.music Parameter 2 (%par2): Return Value Variable:%apprun ]
A2: Wait Until [ MS:250 Seconds:0 Minutes:0 Hours:0 Days:0 ] If [ %apprun Is Set ]
A3: Flash [ Text:%apprun Long:Off ]
Have I missed something? I always get an ExitErr for the first Task. Thanks for help
Do you have busybox installed?
First, oom_adj may not ~ 0/2 EVEN IF IT IS RUNNING or not! You are not matching the correct patterns to determine if the action is successful or not. Research this.
Second, assign a variable to store errors in, something local, and add alert. Flash %somethinglocal if %somethinglocal is set.
That will help you debug.
Also, what are you getting in the results field?
I will ask again, do you have busybox installed?
Very true! I discovered certain apps on my device have a value of LESS THAN ZERO when running in the foreground. Most devices the oom_adj value will = 1 or 0 while in foreground.
Near as I can tell, this device is the only one I have heard of to generate that value for a foreground app. It appears to have something to do with the processes in use for the activity on my device. For example, I can watch an app run from background to foreground as the value shifts from 10 to 6 to < 2 into foreground.
But, regardless you do need to test this. Something copied from someone else does not guarantee it will work for you, even when it does for them :-)
Hello,I need to have my LTE turned off while i'm on my IRC app at work. My building get low lte signal and sometimes it will switch to LTE. it will disconnect me form the IRC server every time.I've tried to have the context @ work (location) and Application -AndroIRC. Since my Rom doesn't support LTE toggle i just have it open the mobile data settings on the enter task and the exit task when i'm done using AndroIRC. the problem is it only recognizes the app while it is opened and not that its running in the background and on the ongoing notifications. basically every time i open the app it will pull up the settings screen and also when i close the settings it tries to open it back up because I've returned AndroIRC what kind of work around is there. or is there a way to have Tasker recognize when com.androirc is running? I've been looking in the guide haven't found anything use to my situation.Thanks,-Dustin