当前景(上)活动(应用程序)的变化如何被通知应用程序、前景、通知

2023-09-12 04:26:56 作者:划破寂寞的天空

我想写的就是通知当前的前台活动的变化,当Android平台的服务。 基本服务应该做一些任务只有在顶变化。 有没有什么办法来订阅和当这种事件发生时得到通知? 或者有没有可能和服务应经常进行轮询来时正在运行活动的清单,并检查什么是前台活动?不是preferable解决方案...

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

推荐答案

AFAIK有两种方法可以做到这一点。

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