Detecting current foreground application

2,946 views
Skip to first unread message

Bateman

unread,
Nov 15, 2011, 2:22:02 PM11/15/11
to Android Linux Kernel Development
Hi,
I want to detect the currently executing foreground app. If the
foreground app is say "email" then perform some tasks.
I tried doing this from within the android frame work. However, the
only way to do it is using Activity Manager and checking the running
tasks periodically from a background service. However, running this
background service periodically will not be good as it will consume
battery and might hog the cpu if the check period is low.

Instead of doing it from the android framework, is there any way I can
do this from within the kernel?
I can check the list of tasks in the run-queue. I can also check what
task is being context-switched in.
However, even while an app is running it will be getting context-
switched in and out. Hence I will have to differentiate the context-
switch in for the first time and the context-switches occurring during
the execution of the application.
Can anyone give me some feedback on this.

Thanks.

Hemanth(ヘマント)

unread,
Nov 18, 2011, 5:46:30 AM11/18/11
to Android Linux Kernel Development
I just posted the link below in another thread.
http://stackoverflow.com/questions/6363207/android-background-service-to-determine-foreground-application
It might be useful for you too.

Hemanth(ヘマント)

unread,
Nov 18, 2011, 5:58:15 AM11/18/11
to Android Linux Kernel Development
Ah, I type faster than I read.You explicitly stated that you are
looking for something in the kernel to do it!

I think doing this from the kernel is actually more difficult.
Ultimately, it might be simpler and faster to use the framework to
lookup the foreground process.
Implemented well, it should not be much of a drain on the battery.
Also it might be better to look for the foreground app only when the
screen is on (depending on your usecase)
However there is the problem that you might be responding to the
change in the foreground app a bit late, depending on your check
period.
Compared to the alternative of modifying the framework or the kernel
it seems to be minimally invasive, and probably good enough?

As for using the kernel, as far as I know there is not easy way of
getting context switch information without enabling some debug flags
for the kernel, which would probably be a bad idea because of
performance and code size increase.

On Nov 16, 12:22 am, Bateman <7thharmo...@gmail.com> wrote:

Bateman

unread,
Dec 4, 2011, 1:29:04 AM12/4/11
to Android Linux Kernel Development
Thanks for the information hemanth.

Doing a periodic check using a background service might be a drain on
the battery and also will introduce a latency in detecting the
foreground app.
I in fact prefer hacking the kernel. I have looked at sched.c and
sched.h, however I have not yet found any flag that denotes an app as
foreground or background.
Any information will be helpful.

Thanks.

Reply all
Reply to author
Forward
0 new messages