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

谁都会走 提交于 2019-11-27 12:19:15

问题


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...


回答1:


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



回答2:


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..............



来源:https://stackoverflow.com/questions/3871119/how-to-be-notified-when-foreground-top-activity-application-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!