Basically what we do is we get a list of running apps and count them, then we check if there more than 0 apps running on the background. after than we simply check to make sure our own app isn't going to be killed after the code execution.
OPTIONAL: I Suggest using a try catch in case something goes wrong.
ActivityManager activityManager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runAppList = activityManager.getRunningAppProcesses();
int listsize = runAppList.size();
if (runAppList != null) {
for (int i = 0; i < listsize; ++i) {
if (runAppList.get(i).pid != android.os.Process.myPid()) {
android.os.Process.killProcess(runAppList.get(i).pid);
activityManager.killBackgroundProcesses(runAppList.get(i).processName);
}
}
}
1 Comment
Great! but what is the minimum API level for that, does it worked on API 8 or higher?