Is there any way to detect an app is running on screen? This means the app the user is working with and is visible to the user. Thanks in advance!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The previous answer is a good general purpose solution but does require the additional permission and may not exactly handle your requirement for knowing if something is visible. If youre looking to do this from inside your own app, an alternative might be to track the visibility of your activities using the activity lifecycle Android provides.
Note that Activity.onPause happens when activity is about to become no longer visible to user. (from: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle)
It’s tempting to use onStop to detect non-visibility, but this is often fired AFTER the next activity is already visible.
Conversely, onResume can be used to track that an activity is visible.
You could use these behaviors via a subclassing of Activity along with an implementation of android.app.application to keep track of whether or not any of your activities are visible.
For example:
The Application class used to do tracking:
To use this adjust the application tag in your manifest to reference your Application class as in:
The BaseActivity that all of your Activities will derive from:
And finally, an example Activity that extends BaseActivity:
Whenever you want to know if something is visible (probably from some service or thread in your app) you only need a Context to get at the Application and then can call isAnyActivityVisible() as in: