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:Main.java

public static void shutDownPck(Context context, String pckName) {
    if (context != null) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        am.killBackgroundProcesses(pckName);
    }//from w ww .j  a va2  s .  co m
}

From source file:Main.java

public static void stopRunningProcess(String componentName, Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
    am.killBackgroundProcesses(componentName);
}

From source file:Main.java

/**
 * Exit current app(clear memory). This util needs
 * `android.permission.KILL_BACKGROUND_PROCESSES` permission.
 *
 * @param context context//  w  w  w  . jav  a2s .c om
 */
public static void exitApp(Context context) {
    if (context == null) {
        return;
    }

    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    manager.killBackgroundProcesses(context.getPackageName());
    System.exit(0);
}

From source file:Main.java

public static void killProcess(Context ctx, String pkgName) {
    ActivityManager actManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    // version 1.5 - 2.1
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        actManager.restartPackage(pkgName);
    }//w  ww . j a va  2  s  . c  o m
    // version 2.2+
    else {
        actManager.killBackgroundProcesses(pkgName);
    }
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void killProgram(Context context, String packageName) {
    ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    try {/*from w ww .  j ava2  s  . c  om*/
        // 2.1 version
        mActivityManager.restartPackage(packageName);
    } catch (Exception e) {
        // 2.2 version
        mActivityManager.killBackgroundProcesses(packageName);
    }
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName;/*from  w w  w  .  ja  va 2 s. c  o m*/
    try {
        if (!processName.contains(":")) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }
        activityManager.killBackgroundProcesses(packageName);
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.nhn.android.archetype.base.AABaseApplicationOrg.java

/**
 *   /*from   w  w  w .java 2 s.c  o  m*/
 */
public static void applicationKill() {
    ActivityManager am = (ActivityManager) _internalInstance.getSystemService(Activity.ACTIVITY_SERVICE);
    am.killBackgroundProcesses(_internalInstance.getPackageName());
}

From source file:Main.java

static void stopSelfService(ActivityManager activityManger) {
    List<ActivityManager.RunningServiceInfo> appProcessList = activityManger.getRunningServices(30);
    for (ActivityManager.RunningServiceInfo appProcessInfo : appProcessList) {
        if (-1 != appProcessInfo.process.indexOf("com.wxlh.sptas:tools")) {
            ComponentName service = appProcessInfo.service;
            activityManger.killBackgroundProcesses(service.getPackageName());
        }//from   ww w  .  jav  a  2  s  .  c  o m
    }
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {
    /*String cmd = "kill -9 "+pid;
    Process process = null;/*from  w w w . j av  a2s  .  co m*/
    DataOutputStream os = null;
    try {
      process = Runtime.getRuntime().exec("su"); 
      os = new DataOutputStream(process.getOutputStream());
      os.writeBytes(cmd + "\n");
      os.writeBytes("exit\n");
      os.flush();
      process.waitFor();
    } catch (Exception e) {
      e.printStackTrace();
    }
    AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);*/

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = null;
    try {
        if (processName.indexOf(":") == -1) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }

        activityManager.killBackgroundProcesses(packageName);

        //
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {

    String cmd = "kill -9 " + pid;
    String Command = "am force-stop " + processName + "\n";
    Process sh = null;//from   ww  w  .jav  a 2 s.com
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        os.writeBytes(Command + "\n");
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        sh.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);
    Log.i("AppUtil", processName);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = null;
    try {
        if (processName.indexOf(":") == -1) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }

        activityManager.killBackgroundProcesses(packageName);

        //
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }

}