In my app I want to know that when another app that is installed on the phone is executing and when it's not. Can you introduce me a function or and class to make the trick? Thank for your help.
Asked
Active
Viewed 40 times
0
-
1possible duplicate of [How to get the list of running applications?](http://stackoverflow.com/questions/3304685/how-to-get-the-list-of-running-applications) – weston Sep 07 '15 at 08:05
1 Answers
0
public boolean isInstalled(Context context, String packageName)
{
PakageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packageName, 0);
return true;
} catch (PackageNotFoundException ex) {
return false;
}
}
public boolean isRunning(Context context, String packageName)
{
ActivityManager am = (ActivityManager) context.getSystemService(Context.Activity_SERVICE);
for (RunningAppProcessInfo info : am.getRunningAppProcesses()) {
for (String runPack : info.pkgList) {
if (runPack.equals(packageName)) return true;
}
}
return false;
}
You need a permission "android.permission.GET_TASKS" to access ActivityManager.

cyanide
- 3,885
- 3
- 25
- 33
-
Starting with Android 5.1.1, this method will only return the processes of your app so this method won't work. – BladeCoder Sep 07 '15 at 09:14
-
I want this can work when my app is close and that app is execute. the above answer can work for this ? – ali reza M Sep 07 '15 at 13:29