Android How to - Is App Running








Question

We would like to know how to is App Running.

Answer

//from  ww w  . j  a va 2  s  .c  o m
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;

public class Main {
  public static boolean isAppRunning(Context context) {
    boolean isRunning = false;
    
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> list = am.getRunningTasks(100);
    if (list.size() <= 0) {
      return false;
    }
      
    String packageName = context.getPackageName();
    for (RunningTaskInfo info : list) {
      if (info.topActivity.getPackageName().equals(packageName)) {
        isRunning = true;
        break;
      }
    }
    
    return isRunning;
  }

}