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:net.iamyellow.gcmjs.GCMIntentService.java

public static boolean isInForeground() {
    Context context = TiApplication.getInstance().getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = context.getPackageName();
    if (am.getRunningTasks(1).get(0).topActivity.getPackageName().equals(packageName)) {
        return true;
    }/* ww  w  .  j av  a 2  s.  c o m*/
    return false;
}

From source file:com.mobivery.greent.MVYBatteryManagerActivityFragment.java

public static boolean isApplicationBroughtToBackground(final Activity activity) {
    ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);

    // Check the top Activity against the list of Activities contained in the Application's package.
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        try {/* ww  w . jav a  2 s . c o m*/
            PackageInfo pi = activity.getPackageManager().getPackageInfo(activity.getPackageName(),
                    PackageManager.GET_ACTIVITIES);
            for (ActivityInfo activityInfo : pi.activities) {
                if (topActivity.getClassName().equals(activityInfo.name))
                    return false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return false; // Never happens.
        }
    }
    return true;
}

From source file:Main.java

/**
 * @param context/*from  w ww.ja v a  2 s .co  m*/
 * @return String
 * @throws
 * @Title: getActivityName
 * @Description: TODO
 */
public static String getActivityName(Context context) {
    collectUtilContext = context;
    if (collectUtilContext == null) {
        return "";
    }
    ActivityManager am = (ActivityManager) collectUtilContext
            .getSystemService(collectUtilContext.ACTIVITY_SERVICE);
    if (checkPermission(collectUtilContext, "android.permission.GET_TASKS")) {
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        return cn.getShortClassName();
    }
    return "";
}

From source file:net.wequick.small.Small.java

public static void preSetUp(Application context) {
    sContext = context;//from w w w.j a  v  a  2  s.c o m

    // Register default bundle launchers
    registerLauncher(new ActivityLauncher());
    registerLauncher(new ApkBundleLauncher());
    registerLauncher(new WebBundleLauncher());

    PackageManager pm = context.getPackageManager();
    String packageName = context.getPackageName();

    // Check if host app is first-installed or upgraded
    int backupHostVersion = getHostVersionCode();
    int currHostVersion = 0;
    try {
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        currHostVersion = pi.versionCode;
    } catch (PackageManager.NameNotFoundException ignored) {
        // Never reach
    }

    if (backupHostVersion != currHostVersion) {
        sIsNewHostApp = true;
        setHostVersionCode(currHostVersion);
    } else {
        sIsNewHostApp = false;
    }

    // Collect host certificates
    try {
        Signature[] ss = pm.getPackageInfo(Small.getContext().getPackageName(),
                PackageManager.GET_SIGNATURES).signatures;
        if (ss != null) {
            int N = ss.length;
            sHostCertificates = new byte[N][];
            for (int i = 0; i < N; i++) {
                sHostCertificates[i] = ss[i].toByteArray();
            }
        }
    } catch (PackageManager.NameNotFoundException ignored) {

    }

    // Check if application is started after unexpected exit (killed in background etc.)
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ComponentName launchingComponent = am.getRunningTasks(1).get(0).topActivity;
    ComponentName launcherComponent = pm.getLaunchIntentForPackage(packageName).getComponent();
    if (!launchingComponent.equals(launcherComponent)) {
        // In this case, system launching the last restored activity instead of our launcher
        // activity. Call `setUp' synchronously to ensure `Small' available.
        setUp(context, null);
    }
}

From source file:cn.whereyougo.framework.utils.PackageManagerUtil.java

/**
 * ??activity//from  ww  w  .j a  v a2 s  .  co  m
 * 
 * @param context
 * @return
 */
@SuppressWarnings("deprecation")
public static ComponentName getTheProcessBaseActivity(final Context context) {
    ActivityManager activityManager = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    RunningTaskInfo task = activityManager.getRunningTasks(1).get(0);
    if (task.numActivities > 0) {
        LogUtil.d(TAG, "runningActivity topActivity=" + task.topActivity.getClassName());
        LogUtil.d(TAG, "runningActivity baseActivity=" + task.baseActivity.getClassName());
        return task.baseActivity;
    }
    return null;
}

From source file:com.wbtech.common.CommonUtil.java

/**
 * ??activity??/*from   w  w  w . ja  v a 2  s  .co  m*/
 * @param context
 * @return
 */
public static String getActivityName(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (checkPermissions(context, "android.permission.GET_TASKS")) {
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        return cn.getShortClassName();
    } else {
        if (UmsConstants.DebugMode) {
            Log.e("lost permission", "android.permission.GET_TASKS");
        }

        return null;
    }

}

From source file:com.wbtech.common.CommonUtil.java

/**
 * PackageName/*w  w  w  . j  a  va  2s .c  o m*/
 * @param context
 * @return
 */
public static String getPackageName(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    if (checkPermissions(context, "android.permission.GET_TASKS")) {
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        return cn.getPackageName();
    } else {
        if (UmsConstants.DebugMode) {
            Log.e("lost permission", "android.permission.GET_TASKS");
        }

        return null;
    }

}

From source file:com.radiusnetworks.scavengerhunt.ScavengerHuntApplication.java

public static boolean isApplicationSentToBackground(final Context context) {
    if (context != null) {
        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  v  a  2  s . com*/
        }
    }
    return false;
}

From source file:org.restcomm.app.qoslib.Utils.QosAPI.java

static public boolean isRunning(Context context, String PackageName) {
    // Get the Activity Manager
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    // Get a list of running tasks, we are only interested in the last one,
    // the top most so we give a 1 as parameter so we only get the topmost.
    List<ActivityManager.RunningTaskInfo> rtasks = manager.getRunningTasks(10000);
    // Get the info we need for comparison.
    for (ActivityManager.RunningTaskInfo task : rtasks) {
        ComponentName c = task.baseActivity;
        if (c != null && c.toString().indexOf(PackageName) >= 0)
            return true;
    }// www.j a  v a 2s . c o m

    //        List<ActivityManager.AppTask> tasks = manager.getAppTasks();
    //
    //        // Get the info we need for comparison.
    //        for (ActivityManager.AppTask task:tasks) {
    //            ComponentName c = task.getTaskInfo().origActivity;
    //            if (c == null && c.equals(watchActivity))
    //                return true;
    //        }

    // If not then our app is not on the foreground.
    return false;
}

From source file:com.nxp.ltsm.ltsmclient.tools.Utils.java

static String getForeGroundKK(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
    // get the info from the currently running task
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

    Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
    String s = taskInfo.get(0).topActivity.getClassName();

    ComponentName componentInfo = taskInfo.get(0).topActivity;
    componentInfo.getPackageName();/*from  w w w . j  a  v a 2 s.  com*/
    return componentInfo.getPackageName();
}