360 Desktop, Kingsoft Cleaning Master, etc. all provide one-click cleaning, one-click acceleration and other functions. In fact, it is to kill some background processes to achieve the purpose of freeing memory.
The basic idea is to list all running processes and view their important values (the larger the value means the lower the importance of the process). You can set a threshold. If the important value of the process is greater than the threshold, you can kill the process.
The important values of the process are as follows:
/** * Constant for {@link #importance}: this is a persistent process. * Only used when reporting to process observers. * @hide */ public static final int IMPORTANCE_PERSISTENT = 50; /** * Constant for {@link #importance}: this process is running the * foreground UI. */ public static final int IMPORTANCE_FOREGROUND = 100; /** * Constant for {@link #importance}: this process is running something * that is actively visible to the user, though not in the immediate * foreground. */ public static final int IMPORTANCE_VISIBLE = 200; /** * Constant for {@link #importance}: this process is running something * that is considered to be actively perceptible to the user. An * example would be an application performing background music playback. */ public static final int IMPORTANCE_PERCEPTIBLE = 130; /** * Constant for {@link #importance}: this process is running an * application that can not save its state, and thus can't be killed * while in the background. * @hide */ public static final int IMPORTANCE_CANT_SAVE_STATE = 170; /** * Constant for {@link #importance}: this process is contains services * that should remain running. */ public static final int IMPORTANCE_SERVICE = 300; /** * Constant for {@link #importance}: this process process contains * background code that is expendable. */ public static final int IMPORTANCE_BACKGROUND = 400; /** * Constant for {@link #importance}: this process is empty of any * actively running code. */ public static final int IMPORTANCE_EMPTY = 500;
Permissions required:
<uses-permission android:name=".KILL_BACKGROUND_PROCESSES"/>
The specific operation code is as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class CleanProcessActivity extends Activity { private static final String TAG = "Clean"; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_clean_process); } public void clean(View v){ //To change body of implemented methods use File | Settings | File Templates. ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> infoList = (); List<> serviceInfos = (100); long beforeMem = getAvailMemory(this); (TAG, "-----------before memory info : " + beforeMem); int count = 0; PackageManager pm = getPackageManager(); if (infoList != null) { for (int i = 0; i < (); ++i) { RunningAppProcessInfo appProcessInfo = (i); (TAG, "process name : " + ); //importance The importance of this process is divided into several levels, and the lower the value, the more important it is. (TAG, "importance : " + ); // Generally, processes with values greater than RunningAppProcessInfo.IMPORTANCE_SERVICE have been useless or empty for a long time. // Generally, processes with a value greater than RunningAppProcessInfo.IMPORTANCE_VISIBLE are non-visible processes, that is, they are running in the background. if ( > RunningAppProcessInfo.IMPORTANCE_VISIBLE) { String[] pkgList = ; for (int j = 0; j < ; ++j) {//pkgList gets the package name running under the process String appName = null; try { appName = (String) ((pkgList[j], 0)); } catch (NameNotFoundException e) { // TODO Auto-generated catch block (); } (TAG, "It will be killed, package name : " + pkgList[j]+" -- "+appName ); (pkgList[j]); count++; } } } } long afterMem = getAvailMemory(this); (TAG, "----------- after memory info : " + afterMem); (this, "clear " + count + " process, " + (afterMem - beforeMem) + "M", Toast.LENGTH_LONG).show(); } private long getAvailMemory(CleanProcessActivity cleanProcessActivity) { // Get the current available memory size of Android ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo mi = new MemoryInfo(); (mi); //; available memory of the current system //return (context, );// Normalize the retrieved memory size (TAG, "Available memory---->>>" + / (1024 * 1024)); return / (1024 * 1024); } }
Notice:
The threshold I selected here is IMPORTANCE_VISIBLE level, that is, non-visible background processes and services will be killed (except for some system processes).
The cleaning effect is similar to the one-click cleaning effect of Kingsoft Cleaning Master and 360 desktop.
If you don't want to kill too fiercely, you can choose the IMPORTANCE_SERVICE level to kill those processes that have not been useful or empty for a long time. The cleaning force at this level is not strong enough to achieve the effect of Kingsoft Cleaning Master.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.