Example usage for android.app ActivityManager killBackgroundProcesses

List of usage examples for android.app ActivityManager killBackgroundProcesses

Introduction

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

Prototype

@RequiresPermission(Manifest.permission.KILL_BACKGROUND_PROCESSES)
public void killBackgroundProcesses(String packageName) 

Source Link

Document

Have the system immediately kill all background processes associated with the given package.

Usage

From source file:com.appassit.common.Utils.java

public static void clearBackMemory() {
    ActivityManager activityManager = (ActivityManager) SLAppication.getContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    int currentProcessId = android.os.Process.myPid();
    RunningAppProcessInfo runningAppProcessInfo = null;
    List<RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
    if (runningAppProcessInfos != null) {
        for (int i = 0; i < runningAppProcessInfos.size(); ++i) {
            runningAppProcessInfo = runningAppProcessInfos.get(i);
            // RunningAppProcessInfo.IMPORTANCE_SERVICE
            // ?/*from www.  ja va 2 s  . co m*/
            // RunningAppProcessInfo.IMPORTANCE_VISIBLE
            // ???,????
            if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE
                    && runningAppProcessInfo.pid != currentProcessId) {
                String[] pkgList = runningAppProcessInfo.pkgList;
                for (int j = 0; j < pkgList.length; ++j) {
                    activityManager.killBackgroundProcesses(pkgList[j]);
                    // count++;
                }
            }
        }
    }
}

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

public static String uiRefreshCommand2(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    // For better reliability, we execute the UI refresh while on the home screen
    Intent homeIntent = new Intent(Intent.ACTION_MAIN);
    homeIntent.addCategory(Intent.CATEGORY_HOME);
    homeIntent.addCategory(Intent.CATEGORY_DEFAULT);
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(homeIntent);//from  ww  w  .  j a v  a  2s  .c o  m

    // Kill all background processes, in order to fully refresh UI
    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if (!packageInfo.packageName.equalsIgnoreCase(context.getPackageName()))
            am.killBackgroundProcesses(packageInfo.packageName);
    }

    // Get launcher package name
    final ResolveInfo mInfo = pm.resolveActivity(homeIntent, 0);

    return "sleep 1 && am force-stop " + mInfo.activityInfo.applicationInfo.packageName;
}

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 ww .  j av a  2  s  .  c om
                        activityManger.killBackgroundProcesses(pkgList[k]);
                        if (recover) {
                            recoverylist.add(pkgList[k]);
                        }
                    }
                }

            }
        }
}

From source file:com.findcab.activity.LocationOverlay.java

/**
 * /*from   w ww  .j  av  a  2 s .  c  o  m*/
 * 
 * @param context
 */
public void exitPro(Context context) {

    // ?Application
    String packName = context.getPackageName();
    ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    activityMgr.restartPackage(packName);
    activityMgr.killBackgroundProcesses(packName);
    android.os.Process.killProcess(android.os.Process.myPid());
}

From source file:com.air.mobilebrowser.ActivityWatchService.java

/**
 * Hide any application that is in the foreground and is not
 * in the white-list, and collapse the status bar.
 *///  w  w w.  j  a  v  a2 s .  com
private void hideApplications() {
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);

    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;

        String packageName = topActivity.getPackageName();

        if (!packageName.equals(getPackageName()) && !mApplicationWhitelist.contains(packageName)) {
            //We hide the activity, by bringing the BrowserActivity back in front.
            Intent intent = new Intent(this, BrowserActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

            //Visual indicator that we are hiding an app.
            Toast.makeText(this, "Blocked " + packageName, Toast.LENGTH_SHORT).show();
        }
    }

    try {
        final Object service = getSystemService("statusbar");
        Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
            final Method collapse = statusbarManager.getMethod("collapse");
            collapse.setAccessible(true);
            collapse.invoke(service);
        } else {
            final Method collapse = statusbarManager.getMethod("collapsePanels");
            collapse.setAccessible(true);
            collapse.invoke(service);
        }
    } catch (Exception ex) {
        Log.e(TAG, "Failed to collapse status bar");
    }

    List<RunningAppProcessInfo> appList = am.getRunningAppProcesses();

    for (RunningAppProcessInfo info : appList) {
        am.killBackgroundProcesses(info.processName);
    }

    appList = am.getRunningAppProcesses();

    for (RunningAppProcessInfo info : appList) {
        if (mAppBlacklist.contains(info.processName)) {
            Intent intent = new Intent(super.getResources().getString(R.string.intent_black_logtag));
            LocalBroadcastManager.getInstance(super.getApplicationContext()).sendBroadcast(intent);
        }
    }

}