Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be notified when foreground (top) activity (application) changes

Tags:

I want to write a service for Android platform that is notified when the current foreground activity changes. Basically the service should do some tasks only when the top activity changes. Is there any way to subscribe and to be notified when this kind of event occurs ? Or there is no possibility and the service should poll from time to time the list of running activities and to check what is the foreground activity ?Not preferable solution...

like image 876
Alex Avatar asked Oct 06 '10 09:10

Alex


People also ask

How do I know if an app is running in the foreground?

Whenever a new activity is started and visible to the user its instance is updated in the application class: activeActivity variable, so we can use it to determine which activity is in the foreground from the notifications layer.

What is allow foreground activity in Android?

A started Service or Activity which user can see and interact is said to be in a foreground state, and the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. The flappy bird activity is foreground because you can see it and interact with it.

How does activity come to the foreground?

Activity or dialog appears in foregroundWhen the covered activity returns to the foreground and regains focus, it calls onResume() . If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state.

What does allow foreground activity means?

Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources. Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services.


1 Answers

AFAIK there are two ways to do that.

  1. Start a service and monitor the Activity Stack, you might check it here
  2. Use an Accessibility Service, you could find a solution here
like image 126
Kevin Avatar answered Oct 20 '22 05:10

Kevin