I am working on an app which needs the information of the apps running at the system up to now. Is there an API/method to retrieve that kind of information?
4 Answers
You cannot detect an App launch in Android, but you can get the list of currently open apps and check if the app you're looking for is open or not using the following code:
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")) {
// Do your stuff here.
}
}
You can also check if the app is running in the foreground using this method
public static boolean isForeground(Context ctx, String myPackage){
ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) {
return true;
}
return false;
}

- 30,560
- 17
- 97
- 143

- 1,443
- 21
- 33
-
3The first code snippet above works great for apps that are running in the background. Thanks. – mbonness Nov 17 '14 at 19:37
-
1It's not working in all cases. For instance, I opened chrome and closed by using home button. And I could see "Chrome" on "app history" / "running apps" list. And I swiped away chrome from there. Still the above code says "Chrome" is running on background. – Manu Apr 28 '15 at 10:14
-
@Manu Maybe the process is still there, then? – android developer Sep 20 '16 at 23:17
-
The code is Running only for the application which is running. I mean i want to get the information of all running apps on device. it shows only the app which is running this code. I send my app in background and run another application but still it did not show the running forground application. – Umair Iqbal Jul 24 '17 at 08:52
-
1I am running the first function and getting only my app, why the other apps are not listed? (I am running kiosk mode) – Meir Feb 13 '18 at 11:33
-
2This wont work for people running Android 6.0 and up. Use UsageStats . It worked for me till Android 7 (even though you need to set permissions manually). – adonayresom Aug 24 '18 at 19:02
public static String getActiveApps(Context context) {
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
String value = u.dateStamp(); // basic date stamp
value += "---------------------------------\n";
value += "Active Apps\n";
value += "=================================\n";
for (ApplicationInfo packageInfo : packages) {
//system apps! get out
if (!isSTOPPED(packageInfo) && !isSYSTEM(packageInfo)) {
value += getApplicationLabel(context, packageInfo.packageName) + "\n" + packageInfo.packageName + "\n-----------------------\n";
}
}
return value;
//result on my emulator
/* 2 Ekim 2017 Pazartesi 14:35:17
---------------------------------
Active Apps
=================================
SystemSetting
com.xyz.systemsetting
-----------------------
myMail
com.my.mail
-----------------------
X-plore
com.lonelycatgames.Xplore
-----------------------
Renotify
com.liamlang.renotify
-----------------------
Mail Box
com.mailbox.email
----------------------- */
}
isSTOPPED
private static boolean isSTOPPED(ApplicationInfo pkgInfo) {
return ((pkgInfo.flags & ApplicationInfo.FLAG_STOPPED) != 0);
}
isSYSTEM
private static boolean isSYSTEM(ApplicationInfo pkgInfo) {
return ((pkgInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
getApplicationLabel
public static String getApplicationLabel(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
String label = null;
for (int i = 0; i < packages.size(); i++) {
ApplicationInfo temp = packages.get(i);
if (temp.packageName.equals(packageName))
label = packageManager.getApplicationLabel(temp).toString();
}
return label;
}

- 141
- 1
- 3
-
5
-
1Using this code, i am getting the apps that are not even running. For example, my working app and only youtube app were running and were present in the stack, but it was not showing the youtube app in the output. It was showing Insta and few other apps that were not even running. Tested this on Samsung S4 Android 5.0.1 – Harry .Naeem Aug 21 '19 at 19:52
-
As of Android 11, this method no longer returns information about all apps; see https://g.co/dev/packagevisibility for details – Sepehr Sep 16 '20 at 13:48
-
-
You can get information about running processes using the ActivityManager class.

- 15,896
- 7
- 53
- 61
MORE MODERN ANSWER (Nov 2022):
This can be handled by the new app usage api
The system collects the usage data on a per-app basis, aggregating the data over daily, weekly, monthly, and yearly intervals. The maximum duration that the system keeps this data is as follows:
Daily data: 7 days Weekly data: 4 weeks Monthly data: 6 months Yearly data: 2 years For each app, the system records the following data:
The last time the app was used
The total length of time the app was in the foreground for that time interval (by > day, week, month, or year)
Timestamp capturing when a component (identified by a package and activity name) ?> moved to the foreground or background during a day
Timestamp capturing when a device configuration changed (such as when the device orientation changed because of rotation)

- 407
- 2
- 7
- 19