tags:

views:

102

answers:

5

Is there any way in android to know if your application is running in background ? By background, I mean none of the applications activities are currently visible to the user ? Any help will be appreciated!!

A: 

I recommend reading through this page: http://developer.android.com/reference/android/app/Activity.html

In short, your activity is no longer visible after onPause() has been called.

Key
I have around 10 activities in my application. So I want to know if none of them if visible to the user. In all, I want to know if my application as a whole is running in background
cppdev
So then you keep track of all 10-ish. Or, as CommonsWare suggested, explain what you are trying to do.
Key
A: 

maybe you see it from the android-market (free) taskill application.

Quandary
+2  A: 

There is no way, short of you tracking it yourself, to determine if any of your activities are visible or not. Perhaps you should consider asking a new StackOverflow question, explaining what it is you are trying to achieve from a user experience, so we can perhaps give you alternative implementation ideas.

CommonsWare
In android, we have a setting called "Background Data". This setting turns of any background data connection when application is running in background. I want to implement "Background data" toggle for my application, so when none of my activities are visible to the user, I would like my service to stop doing any data transfer, but the moment one of my activities resume, I would like to resume data transfer
cppdev
@cppdev: Hopefully, the "data transfer" is being conducted by a `Service`. If so, have your activities notify the service as they appear and disappear. If the `Service` determines that there are no activities visible, and it remains that way for some amount of time, stop the data transfer at the next logical stopping point. Yes, this will require code for each of your activities, but right now, that is unavoidable AFAIK.
CommonsWare
+1  A: 

To piggyback on what CommonsWare and Key have said, you could perhaps extend the Application class and have all of your activities call that on their onPause/onResume methods. This would allow you to know which Activity(ies) are visible, but this could probably be handled better.

Can you elaborate on what you have in mind exactly? When you say running in the background do you mean simply having your application still in memory even though it is not currently on screen? Have you looked into using Services as a more persistent way to manage your app when it is not in focus?

Dan
In android, we have a setting called "Background Data". This setting turns of any background data connection when application is running in background. I want to implement "Background data" toggle for my application, so when none of my activities are visible to the user, I would like my service to stop doing any data transfer, but the moment one of my activities resume, I would like to resume data transfer
cppdev
@Dan: `Application` does not have `onPause()` or `onResume()`.
CommonsWare
@CommonsWare You are right, I was referring to each individual Activity contacting the Application on their pause/resume. This is basically the idea you just shared on the comment to your answer, though you used Services which I imagine is a smarter move.
Dan
@Dan: Oops, sorry, I misread your answer. My bad!
CommonsWare
A: 

You can use this library. I've never used it but it seems to have what you're looking for http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/

Falmarri