问题
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