How to check the Foreground Process from a Service

3788 views
Skip to first unread message

Call_Waiting

unread,
Aug 20, 2010, 4:25:13 PM8/20/10
to Android Developers
Does anyone know how to, or if it's even possible to check the
(foreground process/top of the activity stack) from a service?

Mark Murphy

unread,
Aug 21, 2010, 8:26:15 PM8/21/10
to android-d...@googlegroups.com
On Fri, Aug 20, 2010 at 4:25 PM, Call_Waiting <team.mu...@gmail.com> wrote:
> Does anyone know how to, or if it's even possible to check the
> (foreground process/top of the activity stack) from a service?

I am not aware of a way to "check (foreground process/top of the
activity stack)" anywhere, let alone from a Service.

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 3.1 Available!

Pent

unread,
Aug 22, 2010, 2:35:15 AM8/22/10
to Android Developers
> Does anyone know how to, or if it's even possible to check the
> (foreground process/top of the activity stack) from a service?

It is possible, see the ActivityManager class functions like
getRunningAppProcesses().
getRunningTasks() looks useful but is flakey in my experience e.g.
when you
return to the launcher it is sometimes not updated.

Pent

Dianne Hackborn

unread,
Aug 22, 2010, 4:29:31 PM8/22/10
to android-d...@googlegroups.com
Note that these methods are very much not intended for doing anything except presenting UI to the user about what is running.  Do not rely on them to drive logic in your app.  You will ultimately be unhappy.


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-develop...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Chris Stratton

unread,
Aug 22, 2010, 5:00:14 PM8/22/10
to Android Developers
I had something sort of working once, but it kept doing things like
reporting an ime as the foreground, rather than the activity it was
accepting input on behalf of... which made it useless for my purpose.

On Aug 22, 4:29 pm, Dianne Hackborn <hack...@android.com> wrote:
> Note that these methods are very much not intended for doing anything except
> presenting UI to the user about what is running.  Do not rely on them to
> drive logic in your app.  You will ultimately be unhappy.
>
>
>
>
>
> On Sat, Aug 21, 2010 at 11:35 PM, Pent <tas...@dinglisch.net> wrote:
> > > Does anyone know how to, or if it's even possible to check the
> > > (foreground process/top of the activity stack) from a service?
>
> > It is possible, see the ActivityManager class functions like
> > getRunningAppProcesses().
> > getRunningTasks() looks useful but is flakey in my experience e.g.
> > when you
> > return to the launcher it is sometimes not updated.
>
> > Pent
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-d...@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-develop...@googlegroups.com<android-developers%2Bunsubs cr...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Kostya Vasilyev

unread,
Aug 22, 2010, 5:13:10 PM8/22/10
to android-d...@googlegroups.com

The OP didn't specify whether he needs to track activities of other apps, or his own.

If it's his own activities, it could be as simple as tracking state changes in activities' onStart/onStop or onResume/onPause. More reliable, too.

--
Kostya Vasilyev -- http://kmansoft.wordpress.com

23.08.2010 1:00 пользователь "Chris Stratton" <cs0...@gmail.com> написал:



I had something sort of working once, but it kept doing things like
reporting an ime as the foreground, rather than the activity it was
accepting input on behalf of... which made it useless for my purpose.


On Aug 22, 4:29 pm, Dianne Hackborn <hack...@android.com> wrote:

> Note that these methods are very...

> On Sat, Aug 21, 2010 at 11:35 PM, Pent <tas...@dinglisch.net> wrote:

> > > Does anyone know how to...

> > android-develop...@googlegroups.com<android-developers%2Bunsubs cr...@googlegroups.com>

> > For more options, visit this group at

> >http://groups.google.com/group/android-developers?hl=en...

> hack...@android.com

>
> Note: please don't send private questions to me, as I don't have time to

> provide private suppo...

Pent

unread,
Aug 23, 2010, 12:01:56 AM8/23/10
to Android Developers
> Note that these methods are very much not intended for doing anything except
> presenting UI to the user about what is running.  Do not rely on them to
> drive logic in your app.  You will ultimately be unhappy.

That paragraph should go in the docs IMO, save people finding out the
hard
way.

Pent

Frank Weiss

unread,
Aug 23, 2010, 12:38:53 AM8/23/10
to android-d...@googlegroups.com
I'm curious what the OP was trying to accomplish. It seems to me that
even if the informastion were available, it could be out of date in a
very short matter of time.

FrankG

unread,
Aug 23, 2010, 2:54:33 AM8/23/10
to Android Developers
Hello Frank,

one reason for this kind of question could be, that
his service get an hardware event and would like to start i.e. a
dialog.
But his service is not allowed to hide another Activity.
Such a scenario I could imagine.

Best regards. Frank

nation-x

unread,
Aug 23, 2010, 8:10:24 AM8/23/10
to Android Developers
You can send a broadcast to your foreground process using a receiver
that triggers the foreground process to call a method in the service
from onReceive().

Android Workz

FrankG

unread,
Aug 24, 2010, 2:30:40 AM8/24/10
to Android Developers
Yes, if you have both sides in your hand. But the basis of
my assumption was, that this is not the case.

Example Requirement : Don't pop up the triggered dialog,
if a certain 3rd Party dialog is
already shown.


Good luck ! Frank
> > (foreground process/top of the activity stack) from a service?- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Call_Waiting

unread,
Aug 24, 2010, 8:09:56 PM8/24/10
to Android Developers
I'm just trying to check from a service what's on the top of the
activity stack, because the app starts another app instantly in
onCreate(), from previous settings. I'm looking to kill my service
automatically without having to comeback to my program at all. So I'm
just looking for a way to peek() at the Activity Stack, or another way
about it.

On Aug 22, 2:13 pm, Kostya Vasilyev <kmans...@gmail.com> wrote:
> The OP didn't specify whether he needs to track activities of other apps, or
> his own.
>
> If it's his own activities, it could be as simple as tracking state changes
> in activities' onStart/onStop or onResume/onPause. More reliable, too.
>
> --
> Kostya Vasilyev --http://kmansoft.wordpress.com
>
> 23.08.2010 1:00 пользователь "Chris Stratton" <cs07...@gmail.com> написал:
>
> I had something sort of working once, but it kept doing things like
> reporting an ime as the foreground, rather than the activity it was
> accepting input on behalf of... which made it useless for my purpose.
>
> On Aug 22, 4:29 pm, Dianne Hackborn <hack...@android.com> wrote:
>
> > Note that these methods are very...
> > On Sat, Aug 21, 2010 at 11:35 PM, Pent <tas...@dinglisch.net> wrote:
> > > > Does anyone know how to...
> > > android-develop...@googlegroups.com<android-developers%2Bunsubs cr...@googlegroups.com><android-developers%2Bunsubs

Frank Weiss

unread,
Aug 24, 2010, 8:18:43 PM8/24/10
to android-d...@googlegroups.com
I suppose you could simply have the activity, when it comes to
foreground (onResume), go ahead and stop the service?

Mark Murphy

unread,
Aug 24, 2010, 8:32:40 PM8/24/10
to android-d...@googlegroups.com
On Tue, Aug 24, 2010 at 8:09 PM, Call_Waiting <team.mu...@gmail.com> wrote:
> I'm just trying to check from a service what's on the top of the
> activity stack

You assume there is one activity stack. Each task has a stack:

http://developer.android.com/guide/topics/fundamentals.html#acttask

You have no control over what tasks the user is using, because, like
it or not, users can do what they want.

> because the app starts another app instantly in
> onCreate()

onCreate() of...an activity? a service? a SQLiteOpenHelper? a
ContentProvider? something else?

> I'm looking to kill my service
> automatically without having to comeback to my program at all.

Why is there a service in the first place?

Call_Waiting

unread,
Aug 24, 2010, 8:47:58 PM8/24/10
to Android Developers
Ok maybe, I have the Activity Stack mannerism wrong. But the fact
that when you hit the back button it knows to go from your app, to
another app that's not your's, or to the home screen. Theirs a list
somewhere in memory for it to do this. By checking the foreground
from my service, is knowing that the user can do whatever they want
to. That's how I planned on turning off my app. So you don't have to
comeback to it, to turn it off.

the onCreate that I'm referring about is in my activity.

On Aug 24, 5:32 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> On Tue, Aug 24, 2010 at 8:09 PM, Call_Waiting <team.mu.capt...@gmail.com> wrote:
> > I'm just trying to check from a service what's on the top of the
> > activity stack
>
> You assume there is one activity stack. Each task has a stack:
>
> http://developer.android.com/guide/topics/fundamentals.html#acttask
>
> You have no control over what tasks the user is using, because, like
> it or not, users can do what they want.
>
> > because the app starts another app instantly in
> > onCreate()
>
> onCreate() of...an activity? a service? a SQLiteOpenHelper? a
> ContentProvider? something else?
>
> > I'm looking to kill my service
> > automatically without having to comeback to my program at all.
>
> Why is there a service in the first place?
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy

Frank Weiss

unread,
Aug 24, 2010, 8:58:54 PM8/24/10
to android-d...@googlegroups.com

What does "turning off my app" mean?

Call_Waiting

unread,
Aug 24, 2010, 10:04:51 PM8/24/10
to Android Developers
killing the service = turning off my app

But my app works, and I can turn it off from the activity. I'm just
need a check to see when to trigger it.


I think Hackborn is on the right track with getRunningAppProcesses().


The first time you start my app - input your settings, one of these is
an app which you want to auto-start when you start my app.
Any other time you never see my app, or the settings. - Unless you
press back button, then you get back to my app's settings.
When your in someone else's app, theirs not away to trigger when you
have left that app. So that's what we are trying to find, the
trigger.


If anyone has more info on some example about implementing
getRunningAppProcesses(). It would be much appreciated.

Call_Waiting

unread,
Aug 24, 2010, 10:24:25 PM8/24/10
to Android Developers
Thanks Murphy and Hackborn.

I think I found the answer.

http://developer.android.com/reference/android/app/ActivityManager.RunningTaskInfo.html#topActivity


On Aug 24, 7:04 pm, Call_Waiting <team.mu.capt...@gmail.com> wrote:
> killing theservice= turning off my app

Frank Weiss

unread,
Aug 24, 2010, 10:27:15 PM8/24/10
to android-d...@googlegroups.com
I'll think you need to more precise what that trigger is. Previously,
you've been asking how to know what activty is on top of the stack. It
seems that the activity on top of the stack is the one your activity
started. So I would say the trigger is the moment when your activity
starts the other activity.

Dianne Hackborn

unread,
Aug 24, 2010, 11:14:31 PM8/24/10
to android-d...@googlegroups.com
Please stop saying I am "on the right track."  I am really trying very hard to tell you *not* to do this, because it is not going to be a robust solution.  Not this, not RunningTaskInfo.  These APIs are not there for applications to base their UI flow on, but to do things like show the user the running apps, or a task manager, or such.

Yes there is a list kept in memory for these things.  However, it is off in another process, managed by threads running separately from yours, and not something you can count on (a) seeing in time to make the correct decision or (b) have a consistent picture by the time you return.  Plus the decision about what the "next" activity to go to is always done at the point where the switch is to happen, and it is not until that exact point (where the activity state is briefly locked down to do the switch) that we actually know for such what the next thing will be.

And the implementation and global behavior here is not guaranteed to remain the same in the future.

Have you thought about all of the possible flows through your app like this?  The user pressing back, pressing home, long pressing home to return to the task, bringing down a notification to temporarily look at it and then pressing back to return to the task, etc.  The chances of you getting this all right are really low.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-develop...@googlegroups.com
For more options, visit this group at



--
Dianne Hackborn
Android framework engineer
Reply all
Reply to author
Forward
0 new messages