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.
- Start a service and monitor the Activity Stack, you might check it here
- 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..............