Example usage for android.app ActivityManager getRunningAppProcesses

List of usage examples for android.app ActivityManager getRunningAppProcesses

Introduction

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

Prototype

public List<RunningAppProcessInfo> getRunningAppProcesses() 

Source Link

Document

Returns a list of application processes that are running on the device.

Usage

From source file:com.gm.goldencity.util.Utils.java

public static String getFrontPackageName(Context ctx) {

    ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses();

    return processes.get(0).processName;
}

From source file:com.farmerbb.secondscreen.util.U.java

public static String uiRefreshCommand(Context context, boolean restartActivityManager) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
    int processid = 0;

    if (restartActivityManager) {
        // Kill surfaceflinger if on a Jelly Bean device; run "am restart" if on KitKat or later
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            for (ActivityManager.RunningAppProcessInfo process : pids) {
                if (process.processName.equalsIgnoreCase("/system/bin/surfaceflinger"))
                    processid = process.pid;
            }//w w  w  .  j  a v  a 2  s. co  m

            return "sleep 1 && kill " + Integer.toString(processid);
        } else
            return "sleep 1 && am restart";
    } else {
        // Get SystemUI pid
        for (ActivityManager.RunningAppProcessInfo process : pids) {
            if (process.processName.equalsIgnoreCase("com.android.systemui"))
                processid = process.pid;
        }

        // Starting with 5.1.1 LMY48I, RunningAppProcessInfo no longer returns valid data,
        // which means we won't be able to use the "kill" command with the pid of SystemUI.
        // Thus, if the SystemUI pid gets returned as 0, we need to use the "pkill" command
        // instead, and hope that the user has that command available on their device.
        // Starting with 7.0, pkill doesn't work, so use "kill" and "pidof" instead.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            return "sleep 2 && kill `pidof com.android.systemui`";
        else if (processid == 0)
            return "sleep 2 && pkill com.android.systemui";
        else
            return "sleep 2 && kill " + Integer.toString(processid);
    }
}

From source file:library.artaris.cn.library.utils.SystemUtils.java

/**
 * ????/*w  ww  .ja va 2  s. c om*/
 * @param context
 * @return
 */
public static int gc(Context context) {
    //long i = getDeviceUsableMemory(context);
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null)
        for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;
            } catch (Exception e) {
                e.getStackTrace();
                continue;
            }
        }

    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null)
        for (RunningAppProcessInfo process : processList) {
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        e.getStackTrace();
                        continue;
                    }
                }
            }
        }
    return count;
}

From source file:com.bevyios.gcm.BevyGcmListenerService.java

private boolean applicationIsRunning() {
    ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
        if (processInfo.processName.equals(getApplication().getPackageName())) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String d : processInfo.pkgList) {
                    return true;
                }/*from  ww w . j  av  a2  s .c  o m*/
            }
        }
    }
    return false;
}

From source file:fr.inria.ucn.collectors.AppDataUsageCollector.java

@Override
public void run(Context c, long ts) {
    try {/*from  w w  w.  j ava2s .c o m*/
        // data used per app
        JSONArray parray = new JSONArray();

        File f = new File(PROC_UID_STAT);
        if (f.exists() && f.isDirectory() && f.canRead()) {
            for (String dir : f.list()) {
                parray.put(getProcInfo(c, Integer.parseInt(dir)));
            }
        } else {
            ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
            for (ActivityManager.RunningAppProcessInfo pinfo : am.getRunningAppProcesses()) {
                parray.put(getProcInfo(c, pinfo.uid));
            }
        }

        // done
        Helpers.sendResultObj(c, "app_data_usage", ts, new JSONObject().put("process_list", parray));

    } catch (JSONException jex) {
        Log.w(Constants.LOGTAG, "failed to create json object", jex);
    }
}

From source file:com.honkidenihongo.pre.service.JilFirebaseMessagingService.java

private boolean isAppRunning(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE) {
                return true;
            }//w  w w  .  j a  va 2  s  . c o  m
        }
    }
    return false;
}

From source file:com.alphabetbloc.accessmrs.services.RefreshDataService.java

private boolean isUserEnteringData() {
    RunningAppProcessInfo currentApp = null;
    String accessFormsPackage = "com.alphabetbloc.accessforms";

    boolean enteringData = false;
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
    Iterator<RunningAppProcessInfo> i = l.iterator();

    while (i.hasNext()) {
        currentApp = i.next();//from  ww w .j av a  2  s  . c  o m
        if (currentApp.processName.equalsIgnoreCase(accessFormsPackage)) {
            switch (currentApp.importance) {
            //            case RunningAppProcessInfo.IMPORTANCE_FOREGROUND:
            case RunningAppProcessInfo.IMPORTANCE_VISIBLE:
                enteringData = true;
                break;
            default:
                enteringData = false;
                break;
            }
        }
    }

    return enteringData;
}

From source file:com.avanade.C2DMReceiver.java

@Override
protected void onMessage(Context context, Intent intent) {
    int flag = 0;
    Log.w("C2DMReceiver", intent.getStringExtra("Count"));
    String count = intent.getStringExtra("Count");
    ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    for (int i = 0; i < procInfos.size(); i++) {
        if (procInfos.get(i).processName.equals("com.avanade")) {
            if (count.startsWith("com.avanade")) {
                count = getIntFromEndOfString(count);
                numberNoti = count;/*w  ww.j av a  2s  .c o m*/
                flag = 1;
                break;
            }
        }
    }
    if (flag == 1) {
        Intent pushUpdateIntent = new Intent(getBaseContext(), PushUpdate.class);
        pushUpdateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //pushUpdateIntent.putExtra("unregister", true);
        getApplication().startActivity(pushUpdateIntent);
        flag = 0;
    } else {
        //         System.out.println(" the app is not runnning......do something");
        //Toast.makeText(getApplicationContext(), "Notification Received", Toast.LENGTH_SHORT).show();
    }
}

From source file:justforcommunity.radiocom.utils.MessagingService.java

private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }/*w  w  w.j a v a 2 s  .  co  m*/
    final String packageName = context.getPackageName();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                && appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}

From source file:tm.android.chronos.services.TimerRunner.java

private void setActivityState() {
    //Log.i("Timer Service", "Entering setActivityState at " + ftime.format(System.currentTimeMillis()));
    int importance = 230;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.RunningAppProcessInfo appProcessInfo : activityManager.getRunningAppProcesses())
        for (String pkg : appProcessInfo.pkgList)
            if (pkg.equals(context.getPackageName()))
                importance = appProcessInfo.importance;

    // only this value ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND say that the application is visible on the foreground
    launchNotification = importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
}