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 String getTopActivity(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    @SuppressWarnings("deprecation")
    List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

    return runningTaskInfos == null ? "" : runningTaskInfos.get(0).topActivity.getClassName();
}

From source file:Main.java

/**
 * Indicates if the service given by <tt>activityClass</tt> is currently
 * running.//w  w w  .  j  a  v  a2 s. c o m
 *
 * @param context the Android context
 * @param activityClass the activity class to check
 * @return <tt>true</tt> if the activity given by the class is running,
 * <tt>false</tt> - otherwise
 */
public static boolean isActivityRunning(Context context, Class<?> activityClass) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);

    boolean isServiceFound = false;

    for (int i = 0; i < services.size(); i++) {
        if (services.get(i).topActivity.getClassName().equals(activityClass.getName())) {
            isServiceFound = true;
        }
    }
    return isServiceFound;
}

From source file:Main.java

public static boolean isAppBackground(Context context) {
    if (context != null) {
        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 w w  . j  a  va  2  s  . c o m
        }
    }
    return false;
}

From source file:com.teegarcs.mocker.internals.MockerInternals.java

public static boolean isAppOnForeground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    return taskInfo.get(0).topActivity.getPackageName().equals(context.getApplicationInfo().packageName);
}

From source file:Main.java

public static String getTopActivityName(Context context) {
    String topActivityClassName = null;
    ActivityManager activityManager = (ActivityManager) (context
            .getSystemService(android.content.Context.ACTIVITY_SERVICE));
    List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
    if (runningTaskInfos != null) {
        ComponentName f = runningTaskInfos.get(0).topActivity;
        topActivityClassName = f.getClassName();
    }/*from  w  w  w  .ja va  2  s .  c  o  m*/
    return topActivityClassName;
}

From source file:Main.java

private static ComponentName getTopActivity(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (am == null) {
        return null;
    }//from   w w w .j av  a  2  s  .co  m
    List<RunningTaskInfo> info = am.getRunningTasks(1);
    if (info == null) {
        return null;
    }
    ComponentName activity = info.get(0).topActivity;
    return activity;
}

From source file:Main.java

private static String getTopActivityName(Context context) {
    String topActivityClassName = null;
    ActivityManager activityManager = (ActivityManager) (context
            .getSystemService(android.content.Context.ACTIVITY_SERVICE));
    List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
    if (runningTaskInfos != null) {
        ComponentName f = runningTaskInfos.get(0).topActivity;
        topActivityClassName = f.getClassName();
    }/*www.ja  va  2s  .co m*/
    return topActivityClassName;
}

From source file:Main.java

public static String getTopPackageName(Context context) {
    String topPackageName = null;
    try {/*from  w w  w .  j a  v  a 2 s.  c  o m*/
        ActivityManager activityManager = (ActivityManager) (context
                .getSystemService(android.content.Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
        if (runningTaskInfos != null) {
            ComponentName f = runningTaskInfos.get(0).topActivity;
            topPackageName = f.getPackageName();
        }
    } catch (Exception e) {
    }
    return topPackageName;
}

From source file:Main.java

public static String getTopActivityName(Context context) {
    if (context == null) {
        return "";
    }//from  www  .  ja va 2s.  c o m
    String topActivityClassName = null;
    try {
        ActivityManager activityManager = (ActivityManager) (context
                .getSystemService(android.content.Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
        if (runningTaskInfos != null) {
            ComponentName f = runningTaskInfos.get(0).topActivity;
            topActivityClassName = f.getClassName();
        }
    } catch (Exception e) {
    }
    return topActivityClassName;

}

From source file:Main.java

/**
 * get top activity name//w  ww  . j a va 2  s . c om
 *
 * @param context context of activity
 * @return top activity name
 */
public static String getTopActivity(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    // Note: getRunningTasks is deprecated in API 21(Official)
    List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
    if (runningTaskInfos != null)
        return (runningTaskInfos.get(0).topActivity).toString();
    else
        return null;
}