I want to know if an app (not my app. other app ex. gmail) is running or not.Is it possible using kotlin?
Asked
Active
Viewed 1,096 times
1
-
Here is how you do it in Java: https://stackoverflow.com/questions/4212992/how-can-i-check-if-an-app-running-on-android What specific problems did you encounter when porting that code to Kotlin? It should be pretty straightforward. – Enselic Oct 13 '19 at 07:24
1 Answers
1
Affirmative. It's very much possible to detect if an app, other than your app too, is running or not. You need to use ActivityManager to fetch the information. You can use the following code snippet for achieving the desired list:
val pm = this.getPackageManager()
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
for (rInfo in list) {
Log.w("List of Installed Applications:", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString())
}
If you visit the link mentioned above, you'll find out all the available nested classes here. For example, to fetch the running task, use RunningAppProcessInfo
like this. You can also check if it's in the background, foreground etc. Please refer to this link to get sample codes.

SaadAAkash
- 3,065
- 3
- 19
- 25
-
-
@Enselic I've included the link, please read the whole answer, all the required information nested classes are listed in that link I provided: https://developer.android.com/reference/android/app/ActivityManager.html#summary – SaadAAkash Oct 13 '19 at 05:56
-
For example: `ActivityManager.RunningAppProcessInfo` - Information you can retrieve about a running process - says the links. There's also `ActivityManager.RecentTaskInfo`- Information you can retrieve about tasks that the user has most recently started or visited. All the codes, according to what you require is well documented in the provided link in the answer. Please check again. I've just provided one snippet of code, can be changed into any the nested classes to fetch the associated information. – SaadAAkash Oct 13 '19 at 05:58
-
1See https://stackoverflow.com/help/how-to-answer, links to resources that can be used to figure out the answer is not enough; the answer on this site must be self-contained. Links are only for reference, not a substitute for an elaborate answer. – Enselic Oct 13 '19 at 06:47
-
I've edited the answer to detecting the running processes too, please check and add another answer if mine doesn't fill up the purpose. And also, I still don't think coding the whole android (and then maybe sharing a github url to the entire project) is required to answer such a question on a utility method like this. Please enlighten me if I'm wrong or what else I can add up to this answer to make it better for everyone. – SaadAAkash Oct 13 '19 at 07:29
-
1Unfortunately RunningAppProcessInfo only return my app. It does not work for other apps. Any other solution? I'm using android p. – Avi Khandakar Oct 13 '19 at 10:47