I do understand (from here) that we can use ActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName() to get the current application running in Android; Does Android broadcast any intent when the running application changes? If not how will I know what is the current running application. Let us say at some time t1 I have x as the package name, and at time t2 this value changes, how do I update my x instead of executing ActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName()?
Asked
Active
Viewed 40 times
2 Answers
1
I do understand (from here) that we can use ActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName() to get the current application running in Android
Not on modern versions of Android.
Does Android broadcast any intent when the running application changes?
No.
If not how will I know what is the current running application
Hopefully, you don't, for privacy and security reasons.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
I need to know the current app that is running so that I can send a message to this app. – savi Jul 20 '17 at 21:05
-
@savi: You have no way to "send a message" to apps that you did not write. If you want to know if your *own* apps are in the foreground, you are welcome to set up your own communications for doing that. – CommonsWare Jul 20 '17 at 22:15
1
- Yes you can achieve through running your app in the background, I explained the in this answer and check this too. I am using this and its working fine in AppLocker kind of applications.
- You will not get any broadcast for this, you should run some loop in the background and you should find the currently running applications.

Muthukrishnan Rajendran
- 11,122
- 3
- 31
- 41