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);
    List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

    if (runningTaskInfos != null)
        return runningTaskInfos.get(0).topActivity.getClassName();
    else// w  w  w  .j  a  v a2 s.  c o m
        return "";
}

From source file:Main.java

/**
 * Checks if the application is in the background (i.e behind another application's Activity).
 * /*www . ja va2  s  .  c  om*/
 * @param context
 * @return true if another application is above this one.
 */
public static boolean isApplicationBroughtToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

public static String getTopActivityName(Context context) {
    try {/*from w w  w.  j a v a2 s .  co  m*/
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            return topActivity.getClassName();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static boolean isApplicationForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
    for (ActivityManager.RunningTaskInfo info : services) {
        if (info.topActivity.getPackageName().equalsIgnoreCase(context.getPackageName())) {
            return true;
        }//from   w  w w.java 2  s . co  m
    }
    return false;
}

From source file:Main.java

public static String getTopPackage(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String ret = "";
    try {/*from  www  . j  a v a2s  .  c om*/
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        ret = cn.getPackageName();
    } catch (Exception e) {

    }
    return ret;
}

From source file:Main.java

public static boolean isForeground(Context context, String className) {
    if (context == null || TextUtils.isEmpty(className)) {
        return false;
    }/*from   w  w  w  . ja  v  a 2 s  . c  o m*/

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
    if (list != null && list.size() > 0) {
        ComponentName cpn = list.get(0).topActivity;
        if (className.equals(cpn.getClassName())) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

public static boolean isServiceRunning(Context context, String className) {
    boolean isServiceFound = false;

    try {//from   ww w . jav  a 2s .  com
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).topActivity.getClassName().equalsIgnoreCase(className)) {
                isServiceFound = true;
            }
        }
    } catch (Exception ex) {
    }

    return isServiceFound;
}

From source file:Main.java

public static boolean isApplicationBroughtToBackground(final Context context) {
    try {/*from ww  w.ja v a2  s . c  o m*/
        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;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static boolean isTopActivy(Activity activity) {
    ActivityManager manager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
    String cmpNameTemp = null;/*w ww . ja  v  a2 s .  co m*/

    if (null != runningTaskInfos) {
        cmpNameTemp = (runningTaskInfos.get(0).topActivity).toString();
    }
    return null != cmpNameTemp && cmpNameTemp.equals(activity.getComponentName().toString());
}

From source file:Main.java

/**
 * Checks if the application is in the background (i.e behind another application's Activity).
 *
 * @param context {@link Context}/* w ww .  j ava  2s. c o  m*/
 * @return true if another application is above this one.
 */
public static boolean isApplicationBroughtToBackground(final Context context) {
    if (context.checkCallingOrSelfPermission(Manifest.permission.GET_TASKS) == PackageManager.PERMISSION_DENIED)
        return false;

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())
                && !topActivity.getPackageName().equals("com.google.android.voicesearch")) {
            return true;
        }
    }

    return false;
}