Notify when application comes to foreground

落花浮王杯 提交于 2019-12-11 06:24:06

问题


I have a service which has to monitor which application is in the foreground and based on the application do some functionality.

I can get the list of foreground applications, using ActivityManager.getRunningAppProcesses(). But with this I cannot monitor when the foreground application changes.

ex. when Application 1 which was foreground goes to background and a new Application 2 comes to foreground, the service should be notified about this.

Are there any Broadcasting happening in Android, so that my service can register to that and when there is a change in foreground application, the service gets a notification and can do the functionality as required.

I do not want to do any changes in the Applications... Any possibilities please suggest...


回答1:


Then a simple solution is Create static variable of type Context() in some Class and in all onResume() methods activities initialize it with their Instances.. and check it in service using if conditon and carry on..



来源:https://stackoverflow.com/questions/6385620/notify-when-application-comes-to-foreground

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