public class NotificationDemoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActivityManager am = (ActivityManager) this.getSystemService(this.ACTIVITY_SERVICE); // 获得手机进程列表 List<RunningAppProcessInfo> infos = am.getRunningAppProcesses(); for (RunningAppProcessInfo runningAppProcessInfo : infos) { System.out.println(runningAppProcessInfo.processName); // 去除包含Android包名跟本包名的进程 if(runningAppProcessInfo.processName.indexOf("android") == -1 && runningAppProcessInfo.processName.indexOf(this.getPackageName()) == -1) { // 关闭进程 am.killBackgroundProcesses(runningAppProcessInfo.processName); } } } }
权限增加:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission>
关闭进程时要防止关闭了系统进程及不该禁止的进程