Example usage for android.app ActivityManager getRunningTasks

List of usage examples for android.app ActivityManager getRunningTasks

Introduction

In this page you can find the example usage for android.app ActivityManager getRunningTasks.

Prototype

@Deprecated
public List<RunningTaskInfo> getRunningTasks(int maxNum) throws SecurityException 

Source Link

Document

Return a list of the tasks that are currently running, with the most recent being first and older ones after in order.

Usage

From source file:Main.java

public static boolean isApplicationToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }//from   w w w.  j a va2s. c  o  m
    }
    return false;
}

From source file:Main.java

public static String getRunningActivityName(Context context, String packageName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> processInfos = activityManager.getRunningTasks(10);
    for (ActivityManager.RunningTaskInfo taskInfo : processInfos) {
        if (taskInfo.topActivity != null) {
            if (TextUtils.equals(packageName, taskInfo.topActivity.getPackageName())) {
                return taskInfo.topActivity.getClassName();
            }//www . j a  v  a 2  s .  c  o  m
        }
    }
    return null;
}

From source file:Main.java

/**
 * whether application is in background/*from ww  w  .j a  va2s  .c  om*/
 * <ul>
 * <li>need use permission android.permission.GET_TASKS in Manifest.xml</li>
 * </ul>
 * 
 * @param context
 * @return if application is in background return true, otherwise return
 *         false
 */
public static boolean isApplicationInBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> taskList = am.getRunningTasks(1);
    if (taskList != null && !taskList.isEmpty()) {
        ComponentName topActivity = taskList.get(0).topActivity;
        if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Judge whether an app is in background
 *
 * @param context/*  w  ww  .j  av  a2s .  com*/
 * @return
 */
public static boolean isAppInBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(1);
    if (taskList != null && !taskList.isEmpty()) {
        ComponentName topActivity = taskList.get(0).topActivity;
        if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppForeground(Context context, String packageName) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    @SuppressWarnings("deprecation")
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    return tasks != null && !tasks.isEmpty() && tasks.get(0).topActivity.getPackageName().equals(packageName);
}

From source file:Main.java

/**
 * Used to know if Apollo was sent into the background
 * /*from   w  w  w  . j  a va2  s  .  c o m*/
 * @param context The {@link Context} to use
 */
public static final boolean isApplicationSentToBackground(final Context context) {
    final ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        final ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static ComponentName getTopActivityCompomentName(Context paramContext) {
    List<ActivityManager.RunningTaskInfo> localList = null;
    if (paramContext != null) {
        ActivityManager localActivityManager = (ActivityManager) paramContext.getSystemService("activity");
        if (localActivityManager != null) {
            localList = localActivityManager.getRunningTasks(1);

            if ((localList == null) || (localList.size() <= 0)) {
                return null;
            }/* w  w  w .  j a v a 2s .  c o m*/
        }
    }
    ComponentName localComponentName = localList.get(0).topActivity;
    return localComponentName;
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static boolean isServiceRunning(String activityClass, Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.RunningTaskInfo task : manager.getRunningTasks(Integer.MAX_VALUE)) {
        if (activityClass.equalsIgnoreCase(task.baseActivity.getClassName()))
            return true;
    }//from  w  ww  . j av  a2s.  co m
    return false;
}

From source file:Main.java

public static boolean isAppBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    @SuppressWarnings("deprecation")
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }/*from  w  ww  .  jav  a  2  s.  co  m*/
    }
    return false;
}

From source file:Main.java

private static ComponentName getBaseActivity(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (am == null) {
        return null;
    }/*from w w w  .ja  v a  2s. c  o  m*/
    List<RunningTaskInfo> info = am.getRunningTasks(1);
    if (info == null) {
        return null;
    }
    ComponentName activity = info.get(0).baseActivity;
    return activity;
}