is App Running by checking active service - Android android.content.pm

Android examples for android.content.pm:App

Description

is App Running by checking active service

Demo Code

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;
    }/*from  www .j a  v  a  2s  .co m*/

    String packageName = context.getPackageName();
    for (RunningTaskInfo info : list) {
      if (info.topActivity.getPackageName().equals(packageName)) {
        isRunning = true;
        break;
      }
    }

    return isRunning;
  }

}

Related Tutorials