kill Process - Android Android OS

Android examples for Android OS:Process

Description

kill Process

Demo Code

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;

public class Main {

  public static void killProcess(Context context) {
    ActivityManager activityManger = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
    if (list != null) {
      for (int i = 0; i < list.size(); i++) {
        RunningAppProcessInfo apinfo = list.get(i);
        System.out.println("pid   = " + apinfo.pid);
        System.out.println("processName   = " + apinfo.processName);
        System.out.println("importance   = " + apinfo.importance);
        String[] pkgList = apinfo.pkgList;
        if (apinfo.importance > RunningAppProcessInfo.IMPORTANCE_SERVICE) {
          for (String aPkgList : pkgList) {
            if (!aPkgList.equals("com.elephant.yoyo")) {
              activityManger.killBackgroundProcesses(aPkgList);
            }//from w  w w . jav  a  2  s. c  om
          }
        }
      }
    }
  }

}

Related Tutorials