Tasker to detect application running in background

We used to be told that tasker is only capable of detecting foreground application, if the app gets into background running, it would see it as exit.

Is it? Is there anything we can do to work this out?
Yes, there is a workaround, actually just sorts this out. It proves again how tasker can change our lives.
Okay, let us get into business.

First of all, you may launch an application, eg. QQ for Pad, which I usually play it often in my android pad.
And then, get back to the system settings, tap on the section of application, check running service, allright, you will see the icon of QQ is currently running,
tap again to get into details. Right now, you will be able to see the running process or service for the app. In this case, QQ for Pad, it’s com.tencent.android.pad:service.
Apparently it is a background service.

Stage 2, since we have got the proper running process/service name, what should we do next?
No worries. Open tasker, create a new task, in the action part, select script-run shell, command:
pgrep com.tencent.android.pad:service, use root checked, stored result in %PID.
Please note, pgrep is a linux command, which could get the process ID from the proper package name.
%PID is a user-defined variable to store PID.
Once we got the PID, we can move onto the next stage.

Stage 3, get back to the launcher desktop, open RE explorer, look into the root folder, find the folder of “proc”, tap to get into and pull down the list slowly,
you will see a folder with the name exactly the same as the PID you just got.
Okay, get into this folder, and you will find a file name: oom_adj, open it as a text file, you will see one certain digit, most likely it is 2.
Based on android default RAM control system,
if the digit stored in oom_adj is 1 or 2, it means the app is currently running in background,
if it is greater than 2, it means it is no longer running or already cached.
Well, now, you guys may probably get to know my intention on how to detect an app is running in background.

Stage 4, from this stage, we will see how to get the digit from the proper oom_adj file.
And actually we may simply just put stage 2 which we get PID from app package combined in here.
Tasker, create a task, in the action part, select script-run shell, command:
Cat /proc/$(pgrep com.tencent.android.pad:service)/oom_adj, use root, checked, stored result in %OOMADJ
If you are still confused how exactly to set up the complete profile, please be patient and follow my instruction below.


Example:
Profile:
Context: application: QQ for pad

Action 1: net-mobile data, set on --------this is to open network once qq is running in foreground which tasker application context fired.
Action 2: net-wifi-set on (Optional, depends on if necessary)
Action 3: script-run shell, command: Cat /proc/$(pgrep com.tencent.android.pad:service)/oom_adj, use root, checked, stored result in %OOMADJ
Action 4: task-if, % OOMADJ ~ 1/2
Action 5: task-wait, 20 minutes ----------------- loop in 20 minutes, so it won’t drink the battery too much.
Action 6: variable-variable clear, %OOMADJ
Action 7: task-goto, action number 3 --------------- via this action, even if QQ for Pad is running from foreground into background, the whole tasks part would still working in loop until the app has exited.
Action 8: task-else
Action 9: net-mobile data, set off --------------if QQ is no longer running or cached, just cut the network off to save data stream.
Action 10: net-wifi-set off (Optional, depends on if necessary)


NB.

if the application has registered system broadcast service, or some apps don‘t even have an exit/quit button, even if they do, tap exit, some of them would still remain background service and background process. eg. google map, google play store, etc.


Conclusion, this workaround is available for the apps which don‘t remain background service and process after exited.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。