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.example.finalfcm.MyFirebaseMessagingService.java

private boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }//w  w  w .j  a v a  2s . c  o  m
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }
    return isInBackground;
}

From source file:com.allmycode.flags.other.MyActivityOther.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);// www .  j  a v a 2  s. c  o  m
    targetActivity = (EditText) findViewById(R.id.editText1);
    flags = (EditText) findViewById(R.id.editText2);
    cheatSheet = (EditText) findViewById(R.id.editText3);
    for (String item : flagsList) {
        cheatSheet.append(item);
        cheatSheet.append("\n");
    }
    Intent whoCalled = getIntent();
    helloView = ((TextView) findViewById(R.id.textView3));
    helloView.setTextColor(android.graphics.Color.WHITE);
    helloView.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
    helloView.setText("Flags: 0x" + Integer.toHexString(whoCalled.getFlags()));
    if (whoCalled.getExtras() != null) {
        boolean areErrors = whoCalled.getExtras().getBoolean("existErrors", false);
        if (areErrors) {
            helloView.append(" There were errors!");
            helloView.setTextColor(android.graphics.Color.RED);
            helloView.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        }
    }
    ActivityManager am = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processes;
    processes = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo info : processes) {
        Log.i("Process:", info.processName);
    }
}

From source file:com.allmycode.flags.MyActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/*from  w  ww . java  2  s . c  o  m*/
    targetActivity = (AutoCompleteTextView) findViewById(R.id.editText1);
    ArrayAdapter<String> targetsAdapter = new ArrayAdapter<String>(this, R.layout.list_item, TARGETS_LIST);
    targetActivity.setAdapter(targetsAdapter);
    flags = (AutoCompleteTextView) findViewById(R.id.editText2);
    ArrayAdapter<String> flagsAdapter = new ArrayAdapter<String>(this, R.layout.list_item, FLAGS_LIST_CONCISE);
    flags.setAdapter(flagsAdapter);
    cheatSheet = (EditText) findViewById(R.id.editText3);
    for (String item : FLAGS_LIST) {
        cheatSheet.append(item);
        cheatSheet.append("\n");
    }
    Intent whoCalled = getIntent();
    helloView = ((TextView) findViewById(R.id.textView3));
    helloView.setTextColor(android.graphics.Color.WHITE);
    helloView.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
    helloView.setText("Flags: 0x" + Integer.toHexString(whoCalled.getFlags()));
    if (whoCalled.getExtras() != null) {
        boolean areErrors = whoCalled.getExtras().getBoolean("existErrors", false);
        if (areErrors) {
            helloView.append(" There were errors!");
            helloView.setTextColor(android.graphics.Color.RED);
            helloView.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        }
    }

    ActivityManager am = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processes;
    processes = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo info : processes) {
        Log.i("Process:", info.processName);
    }
}

From source file:org.failedprojects.anjaroot.PackagesFragment.java

public void killProcesses(List<String> pkgs) {
    boolean success = AnJaRoot.gainAccess();
    if (!success) {
        Log.e(LOGTAG, "Failed to gain access, something must be seriously broken...");
        return;// w w w. jav a2 s. co  m
    }

    ActivityManager am = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningapps = am.getRunningAppProcesses();
    for (RunningAppProcessInfo info : runningapps) {
        for (String pkgname : info.pkgList) {
            if (!pkgs.contains(pkgname)) {
                continue;
            }

            Log.v(LOGTAG,
                    String.format("Killing %s with pid %d as access has been revoked", pkgname, info.pid));
            Process.killProcess(info.pid);
        }
    }

    success = AnJaRoot.dropAccess();
    if (!success) {
        Log.e(LOGTAG, "Failed to drop access level");
    }
}

From source file:cl.iluminadoschile.pako.floatingdiv.CustomOverlayService.java

private boolean ingress_running_in_background() {
    String ingress_app_name = this.getApplicationContext().getString(R.string.ingress_process_name);

    ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    boolean running = false;
    for (int i = 0; i < procInfos.size(); i++) {
        String processName = procInfos.get(i).processName;
        if (processName.equals(ingress_app_name)) {
            running = true;/* w w w.  j av a2s .c  o  m*/
        }
    }

    return running;
}

From source file:com.irccloud.android.IRCCloudApplicationBase.java

public void onPause(final Activity context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (notifierSockerTimerTask != null)
        notifierSockerTimerTask.cancel();
    notifierSockerTimerTask = new TimerTask() {
        @Override//from  w  w  w.  j a va2s . c  o m
        public void run() {
            notifierSockerTimerTask = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                for (ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
                    if (info.processName.equals(context.getPackageName())
                            && info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
                        return;
                }
            }
            if (!conn.notifier && conn.getState() == NetworkConnection.STATE_CONNECTED) {
                conn.disconnect();
                if (ServersList.getInstance().count() < 1) {
                    Crashlytics.log(Log.DEBUG, "IRCCloud",
                            "No servers configured, not connecting notifier socket");
                    return;
                }
                if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cm.isActiveNetworkMetered() && cm
                        .getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED)) {
                    try {
                        Thread.sleep(1000);
                        conn.notifier = true;
                        conn.connect();
                    } catch (Exception e) {
                    }
                }
            }
        }
    };

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cm.isActiveNetworkMetered()
            && cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED) {
        notifierTimer.schedule(notifierSockerTimerTask, 5000);
    } else {
        notifierTimer.schedule(notifierSockerTimerTask, 300000);
    }
}

From source file:me.piebridge.prevent.framework.SystemReceiver.java

private Map<String, Set<Long>> getRunningAppProcesses(Context context) {
    Map<String, Set<Long>> running = new HashMap<String, Set<Long>>();
    Set<String> services = getRunningServices(context);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses();
    if (processes == null) {
        PreventLog.w("cannot get running processes");
        return running;
    }/*www  . j a va  2s.  c o  m*/
    for (ActivityManager.RunningAppProcessInfo process : processes) {
        for (String pkg : process.pkgList) {
            Set<Long> importance = running.get(pkg);
            if (importance == null) {
                importance = new LinkedHashSet<Long>();
                running.put(pkg, importance);
            }
            if (process.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) {
                importance.add((long) process.importance);
            } else if (services.contains(pkg)) {
                importance.add((long) process.importance);
            } else {
                importance.add((long) -process.importance);
            }
        }
    }
    return running;
}

From source file:com.alivenet.dmvtaxi.FcmUtil.MyGcmListenerService.java

/**
 * Called when message is received./*from www .j  a v  a  2  s  . co  m*/
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    Log.d(TAG, "From: " + data);
    String message = data.getString("message");
    ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();

    if (message != null) {
        if (message.startsWith("Ride has been cancelled by the driver")) {
            MyApplication.declinedriver = true;
            MyApplication.driverdeclinebookingid = data.getString("bookingId");
            MyApplication.notificationconfiride = false;

            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(getApplicationContext().getPackageName())) {
                            Intent broadcastIntent = new Intent();
                            broadcastIntent.setAction("Driverdecline");
                            sendBroadcast(broadcastIntent);

                        } else {

                        }
                    }
                } else {
                    sendNotification(message);
                }

            }

        }
        if (message.startsWith("Dear")) {

            MyApplication.notimsg = data.getString("message");
            MyApplication.notibookingId = data.getString("bookingId");
            MyApplication.notipUser = data.getString("pUser");
            MyApplication.notisUser = data.getString("sUser");
            splitnotifiaction_flag = true;
            MyApplication.splitride = true;

            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(getApplicationContext().getPackageName())) {
                            Intent broadcastIntent = new Intent();
                            broadcastIntent.setAction("splitrideaccept");
                            sendBroadcast(broadcastIntent);

                        } else {

                        }
                    }
                } else {
                    sendNotification(message);
                }

            }

            /* Intent i = new Intent("AcceptNotificationUIwork");
            sendBroadcast(i);*/

        }
        if (message.equals("ride complete successfully")) {
            MyApplication.radeyourrite = true;
            MyApplication.notificationconfiride = false;
            MyApplication.RateyourRide = false;
            flag = true;
            MyApplication.rideDriverComplete = new RideDriverComplete();
            String driverName = data.getString("driverName");
            String pickLat = data.getString("pickLat");
            String pickLong = data.getString("pickLong");
            String destLat = data.getString("destLat");
            String destLong = data.getString("destLong");
            String pickupaddress = data.getString("pickupaddress");
            String destinationaddress = data.getString("destinationaddress");
            String driverId = data.getString("driverId");
            String vehicle = data.getString("vehicle");
            String totalfare = data.getString("total_fare");
            String rideId = data.getString("rideId");
            MyApplication.RideId = rideId;
            String licenseId = data.getString("licenseId");
            if (message != null) {
                MyApplication.rideDriverComplete.setMessageride(message);
            } else {
                MyApplication.rideDriverComplete.setMessageride("");
            }
            if (driverName != null) {
                MyApplication.rideDriverComplete.setDriverNameride(driverName);
            } else {
                MyApplication.rideDriverComplete.setDriverNameride("");
            }
            if (pickLat != null) {
                MyApplication.rideDriverComplete.setPickLatride(pickLat);
            } else {
                MyApplication.rideDriverComplete.setPickLatride("");
            }
            if (pickLong != null) {
                MyApplication.rideDriverComplete.setPickLongride(pickLong);
            } else {
                MyApplication.rideDriverComplete.setPickLongride("");
            }
            if (destLat != null) {
                MyApplication.rideDriverComplete.setDestLatride(destLat);
            } else {
                MyApplication.rideDriverComplete.setDestLatride("");
            }
            if (destLong != null) {
                MyApplication.rideDriverComplete.setDestLongride(destLong);
            } else {
                MyApplication.rideDriverComplete.setDestLongride("");
            }
            if (driverId != null) {
                MyApplication.rideDriverComplete.setDriverIdride(driverId);
            } else {
                MyApplication.rideDriverComplete.setDriverIdride("");
            }
            if (vehicle != null) {
                MyApplication.rideDriverComplete.setVehicle(vehicle);
            } else {
                MyApplication.rideDriverComplete.setVehicle("");
            }
            if (rideId != null) {
                MyApplication.rideDriverComplete.setRideId(rideId);
            } else {
                MyApplication.rideDriverComplete.setRideId("");
            }
            if (licenseId != null) {
                MyApplication.rideDriverComplete.setLicenseId(licenseId);
            } else {
                MyApplication.rideDriverComplete.setLicenseId("");
            }
            if (totalfare != null) {
                MyApplication.rideDriverComplete.setTotalfare(totalfare);
            } else {
                MyApplication.rideDriverComplete.setTotalfare("");
            }
            if (pickupaddress != null) {
                MyApplication.rideDriverComplete.setPickupaddress(pickupaddress);
            } else {
                MyApplication.rideDriverComplete.setPickupaddress("");
            }
            if (destinationaddress != null) {
                MyApplication.rideDriverComplete.setDestinationaddress(destinationaddress);
            } else {
                MyApplication.rideDriverComplete.setDestinationaddress("");
            }

            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(getApplicationContext().getPackageName())) {
                            MyApplication.pickuploction = false;
                            Intent broadcastIntent = new Intent();
                            broadcastIntent.putExtra("ridedriverInfo", MyApplication.rideDriverComplete);
                            broadcastIntent.setAction("DriverNotificationcomplete");
                            sendBroadcast(broadcastIntent);

                        } else {

                        }
                    }
                }

            }

        }
    }

    for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            for (String activeProcess : processInfo.pkgList) {
                if (activeProcess.equals(getApplicationContext().getPackageName())) {

                } else {
                    System.out.println("App in background state");
                    sendNotification(message);
                }
            }
        } else {
            System.out.println("App in background state");
            sendNotification(message);
        }

    }

}

From source file:edu.iub.seclab.appguardian.AppGuardianService.java

public void killProcessByPkg(String[] result) {
    boolean recover = false;
    //      sendStopBroadcast();
    ActivityManager activityManger = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
    if (list != null)
        for (int i = 0; i < list.size(); i++) {
            ActivityManager.RunningAppProcessInfo apinfo = list.get(i);

            for (int j = 0; j < result.length; j++) {
                mTargetList.getTarget(mActiveTarget).killList.add(result[j]);
                int pid = apinfo.pid;
                if (pid == Integer.parseInt(result[j])) {
                    String[] pkgList = apinfo.pkgList;
                    for (int k = 0; k < pkgList.length; k++) {
                        if (apinfo.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) {
                            recover = true;
                        } else {
                            recover = false;
                        }//from  w w  w.j  a  v a2 s .co m
                        activityManger.killBackgroundProcesses(pkgList[k]);
                        if (recover) {
                            recoverylist.add(pkgList[k]);
                        }
                    }
                }

            }
        }
}

From source file:com.landenlabs.all_devtool.ConsoleFragment.java

private void snapConsole() {
    try {/*w  ww. j a v a 2 s .  c o m*/
        MemoryInfo mi = new MemoryInfo();
        ActivityManager activityManager = (ActivityManager) getActivity()
                .getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);

        consoleState.lastFreeze = new Date();
        consoleState.usingMemory = Debug.getNativeHeapSize();
        consoleState.freeMemory = mi.availMem;
        if (Build.VERSION.SDK_INT >= 16) {
            consoleState.totalMemory = mi.totalMem;
        }

        consoleState.netRxBytes = TrafficStats.getTotalRxBytes();
        consoleState.netRxPacks = TrafficStats.getTotalRxPackets();
        consoleState.netTxBytes = TrafficStats.getTotalTxBytes();
        consoleState.netTxPacks = TrafficStats.getTotalRxPackets();

        ActivityManager actMgr = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
        consoleState.processCnt = actMgr.getRunningAppProcesses().size();
        consoleState.batteryLevel = calculateBatteryLevel(getActivity());
    } catch (Exception ex) {
    }
}