0

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.

Gargaroz
  • 313
  • 9
  • 28
  • 1
    possible 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 Answers1

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