开发者

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

开发者 https://www.devze.com 2023-01-19 08:54 出处:网络
I want to write a service for Android platform that is notified when the current foreground activity changes.

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 acti开发者_StackOverflow社区vity ?Not preferable solution...


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


You should bind every activity to the service and you will know which activity is running.

try this:

List runningTaskInfos=actvityManager.getRunningTasks(1).get(i).topActivity .getPackageName();

this method will give info of activity's package name which is in foreground..............

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号