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 killProcesses(Context context, int pid, String processName) {

    String cmd = "kill -9 " + pid;
    String Command = "am force-stop " + processName + "\n";
    Process sh = null;/* ww  w .  j  a va 2 s . c  om*/
    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) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        sh.waitFor();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);
    // L.i(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();
    }

}

From source file:Main.java

/**
 *//*w  ww . j  a va2s . c  o  m*/
public static void clearEmptyTask(Context context) {
    ActivityManager activityManger = (ActivityManager) context.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);
            String[] pkgList = apinfo.pkgList;
            if (apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY) {// >=300
                android.os.Process.killProcess(apinfo.pid);
                for (int j = 0; j < pkgList.length; j++) {
                    activityManger.killBackgroundProcesses(pkgList[j]);
                }
            }
        }
}

From source file:Main.java

public static void releaseMemory(Context context) {
    ActivityManager activityManger = (ActivityManager) context.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);
            String[] pkgList = apinfo.pkgList;
            if (apinfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE
                    && !apinfo.processName.equals("com.tt.realeasememory")) {
                for (int j = 0; j < pkgList.length; j++) {
                    if (Build.VERSION.SDK_INT >= 8) {
                        activityManger.killBackgroundProcesses(pkgList[j]);
                    } else {
                        activityManger.restartPackage(pkgList[j]);
                    }//from w  ww.  j  a va  2s .c  o  m
                }
            }
        }
    }
}

From source file:Main.java

public static int gc(Context cxt) {
    //long i = getDeviceUsableMemory(cxt);
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.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++;//from w ww  .  j  ava2  s  . c  o  m
            } 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:Main.java

public static int gc(Context cxt) {
    //long i = getDeviceUsableMemory(cxt);
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.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++;//from  w ww.  ja  v a 2  s . c o  m
            } 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:Main.java

/**
 * kill other process and services/*from ww  w .ja  va  2s  .  c o  m*/
 *
 * @param context
 * @return count of clean up process and services
 */
public static int gc(Context context) {
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null) {
        for (ActivityManager.RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }

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

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

public static void applicationAllKill() {
    final ActivityManager am = (ActivityManager) _internalInstance.getSystemService(Activity.ACTIVITY_SERVICE);
    // stop running service inside current process.
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    for (RunningServiceInfo service : serviceList) {
        if (service.pid == android.os.Process.myPid()) {
            Intent stop = new Intent();
            stop.setComponent(service.service);
            _internalInstance.stopService(stop);
        }//from   w  ww  . j  av a 2s. c o m
    }

    // move current task to background.
    Intent launchHome = new Intent(Intent.ACTION_MAIN);
    launchHome.addCategory(Intent.CATEGORY_DEFAULT);
    launchHome.addCategory(Intent.CATEGORY_HOME);
    _internalInstance.startActivity(launchHome);

    // post delay runnable(waiting for home application launching)
    new Handler().postDelayed(new Runnable() {
        public void run() {
            am.killBackgroundProcesses(_internalInstance.getPackageName());
        }
    }, 2000);
}

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

/**
 * ????/*from   w ww  .j av a  2 s .  c o  m*/
 * @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:edu.iub.seclab.appguardian.AppGuardianService.java

public void killProcess(String pkgName) {
    ActivityManager activityManger = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    activityManger.killBackgroundProcesses(pkgName);
}

From source file:com.shivshankar.MyFirebaseMessagingService.java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d("TAGRK", "From: " + remoteMessage.getFrom());
    String message = "", title = "", strImageURL = "";
    if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
        Log.d("TAGRK", "Message data payload: " + remoteMessage.getData());

        message = "" + remoteMessage.getData().get(Config.MESSAGE_KEY);
        title = "" + remoteMessage.getData().get(Config.TITLE_KEY);
        strImageURL = "" + remoteMessage.getData().get(Config.IMAGE_KEY);
    } else if (remoteMessage.getNotification() != null) {
        Log.d("TAGRK", "Message Notification Body: " + remoteMessage.getNotification().getBody());
        message = "" + remoteMessage.getNotification().getBody();
        title = "" + remoteMessage.getNotification().getTitle();
        strImageURL = "" + remoteMessage.getNotification().getIcon();
    }/*w w  w. ja  v a2 s.c  om*/

    if (strImageURL != null && !strImageURL.equals("") && !strImageURL.equals("null")) {
        Handler handler = new Handler(Looper.getMainLooper());
        final String finalTitle = title;
        final String finalMessage = message;
        final String finalStrImageURL = strImageURL;
        handler.post(new Runnable() {
            public void run() {
                new ServerAPICallImageBitmap(finalTitle, finalMessage, finalStrImageURL, "").execute();
            }
        });
    } else if (title.equalsIgnoreCase("Logout")) {
        try {
            SharedPreferences.Editor editor = AppPreferences.getPrefs().edit();
            editor.putString(commonVariables.KEY_LOGIN_ID, "0");
            editor.putBoolean(commonVariables.KEY_IS_LOG_IN, false);
            editor.putString(commonVariables.KEY_SELLER_PROFILE, "");
            editor.putString(commonVariables.KEY_BUYER_PROFILE, "");
            editor.putString(commonVariables.KEY_BRAND, "");
            editor.putBoolean(commonVariables.KEY_IS_BRAND, false);
            editor.putBoolean(commonVariables.KEY_IS_SELLER, false);
            editor.putBoolean(commonVariables.KEY_IS_SKIPPED_LOGIN_BUYER, false);
            editor.commit();
            android.os.Process.killProcess(android.os.Process.myPid());
            List<ApplicationInfo> packages;
            PackageManager pm;
            pm = getPackageManager();
            packages = pm.getInstalledApplications(0);

            ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            String myPackage = getApplicationContext().getPackageName();
            for (ApplicationInfo packageInfo : packages) {
                if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
                    continue;
                if (packageInfo.packageName.equals(myPackage))
                    continue;
                mActivityManager.killBackgroundProcesses(packageInfo.packageName);
            }
            mActivityManager.restartPackage(myPackage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else
        sendNotification(title, message);
    Log.i("TAGRK", "Received: " + remoteMessage.toString());
}