Android 一键清理、内存清理功能实现

来源:互联网 发布:java怎么做对外接口 编辑:程序博客网 时间:2024/04/29 20:31

360桌面、金山清理大师等都提供了一键清理、一键加速等功能,其实就是杀一些后台进程来达到释放内存的目的。

   基本思路就是列出所有运行的进程,查看其重要值(RunningAppProcessInfo.importance,值越大说明进程重要程度越低),可以设定一个阈值,

如果该进程的重要值大于该阈值,就可以杀掉该进程。

进程的重要值有以下几个等级:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.          * Constant for {@link #importance}: this is a persistent process. 
  3.          * Only used when reporting to process observers. 
  4.          * @hide 
  5.          */  
  6.         public static final int IMPORTANCE_PERSISTENT = 50;  
  7.   
  8.         /** 
  9.          * Constant for {@link #importance}: this process is running the 
  10.          * foreground UI. 
  11.          */  
  12.         public static final int IMPORTANCE_FOREGROUND = 100;  
  13.           
  14.         /** 
  15.          * Constant for {@link #importance}: this process is running something 
  16.          * that is actively visible to the user, though not in the immediate 
  17.          * foreground. 
  18.          */  
  19.         public static final int IMPORTANCE_VISIBLE = 200;  
  20.           
  21.         /** 
  22.          * Constant for {@link #importance}: this process is running something 
  23.          * that is considered to be actively perceptible to the user.  An 
  24.          * example would be an application performing background music playback. 
  25.          */  
  26.         public static final int IMPORTANCE_PERCEPTIBLE = 130;  
  27.           
  28.         /** 
  29.          * Constant for {@link #importance}: this process is running an 
  30.          * application that can not save its state, and thus can't be killed 
  31.          * while in the background. 
  32.          * @hide 
  33.          */  
  34.         public static final int IMPORTANCE_CANT_SAVE_STATE = 170;  
  35.           
  36.         /** 
  37.          * Constant for {@link #importance}: this process is contains services 
  38.          * that should remain running. 
  39.          */  
  40.         public static final int IMPORTANCE_SERVICE = 300;  
  41.           
  42.         /** 
  43.          * Constant for {@link #importance}: this process process contains 
  44.          * background code that is expendable. 
  45.          */  
  46.         public static final int IMPORTANCE_BACKGROUND = 400;  
  47.           
  48.         /** 
  49.          * Constant for {@link #importance}: this process is empty of any 
  50.          * actively running code. 
  51.          */  
  52.         public static final int IMPORTANCE_EMPTY = 500;  

直接上代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.android.clearmemory;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ActivityManager;  
  5. import android.app.ActivityManager.MemoryInfo;  
  6. import android.app.ActivityManager.RunningAppProcessInfo;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.Toast;  
  13.   
  14. import java.util.List;  
  15.   
  16. public class ClearMemoryActivity extends Activity {  
  17.     private static final String TAG = "ClearMemoryActivity";  
  18.   
  19.     /** 
  20.      * Called when the activity is first created. 
  21.      */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.   
  27.         Button clear = (Button) findViewById(R.id.clear);  
  28.         clear.setOnClickListener(new View.OnClickListener() {  
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 //To change body of implemented methods use File | Settings | File Templates.  
  32.                 ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
  33.                 List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();  
  34.                 List<ActivityManager.RunningServiceInfo> serviceInfos = am.getRunningServices(100);  
  35.   
  36.                 long beforeMem = getAvailMemory(ClearMemoryActivity.this);  
  37.                 Log.d(TAG, "-----------before memory info : " + beforeMem);  
  38.                 int count = 0;  
  39.                 if (infoList != null) {  
  40.                     for (int i = 0; i < infoList.size(); ++i) {  
  41.                         RunningAppProcessInfo appProcessInfo = infoList.get(i);  
  42.                         Log.d(TAG, "process name : " + appProcessInfo.processName);  
  43.                         //importance 该进程的重要程度  分为几个级别,数值越低就越重要。  
  44.                         Log.d(TAG, "importance : " + appProcessInfo.importance);  
  45.   
  46.                         // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了  
  47.                         // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着  
  48.                         if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {  
  49.                             String[] pkgList = appProcessInfo.pkgList;  
  50.                             for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名  
  51.                                 Log.d(TAG, "It will be killed, package name : " + pkgList[j]);  
  52.                                 am.killBackgroundProcesses(pkgList[j]);  
  53.                                 count++;  
  54.                             }  
  55.                         }  
  56.   
  57.                     }  
  58.                 }  
  59.   
  60.                 long afterMem = getAvailMemory(ClearMemoryActivity.this);  
  61.                 Log.d(TAG, "----------- after memory info : " + afterMem);  
  62.                 Toast.makeText(ClearMemoryActivity.this"clear " + count + " process, "  
  63.                             + (afterMem - beforeMem) + "M", Toast.LENGTH_LONG).show();  
  64.             }  
  65.         });  
  66.   
  67.         ClearMemoryFloatView.instance(getApplicationContext()).createView();  
  68.     }  
  69.   
  70.     //获取可用内存大小  
  71.     private long getAvailMemory(Context context) {  
  72.         // 获取android当前可用内存大小  
  73.         ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
  74.         MemoryInfo mi = new MemoryInfo();  
  75.         am.getMemoryInfo(mi);  
  76.         //mi.availMem; 当前系统的可用内存  
  77.         //return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化  
  78.         Log.d(TAG, "可用内存---->>>" + mi.availMem / (1024 * 1024));  
  79.         return mi.availMem / (1024 * 1024);  
  80.     }  
  81. }  

注意:

我这里选择阈值是IMPORTANCE_VISIBLE级别的,也就是非可见的后台进程和服务会被杀掉(一些系统进程肯定除外)。

清理的效果跟金山清理大师和360桌面的一键清理效果差不多。

如果不想杀的太凶,可以选择IMPORTANCE_SERVICE级别,杀掉那些长时间没用或者空进程了,

这个级别的清理力度不够大,达不到金山清理大师的效果。

2 0
原创粉丝点击