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

转:http://blog.csdn.net/chuyouyinghe/article/details/52037846

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

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

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

[plain]  view plain  copy
  在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;    

需要权限:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>  


具体操作代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.demo;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.app.ActivityManager;  
  7. import android.app.ActivityManager.MemoryInfo;  
  8. import android.app.ActivityManager.RunningAppProcessInfo;  
  9. import android.content.Context;  
  10. import android.content.pm.PackageManager;  
  11. import android.content.pm.PackageManager.NameNotFoundException;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.view.Menu;  
  15. import android.view.MenuItem;  
  16. import android.view.View;  
  17. import android.widget.Toast;  
  18.   
  19. public class CleanProcessActivity extends Activity {  
  20.   
  21.     private static final String TAG = "Clean";  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_clean_process);  
  27.     }  
  28.     public void clean(View v){  
  29.         //To change body of implemented methods use File | Settings | File Templates.    
  30.         ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);    
  31.         List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();    
  32.         List<ActivityManager.RunningServiceInfo> serviceInfos = am.getRunningServices(100);    
  33.   
  34.         long beforeMem = getAvailMemory(this);    
  35.         Log.d(TAG, "-----------before memory info : " + beforeMem);    
  36.         int count = 0;    
  37.         PackageManager pm = getPackageManager();  
  38.           
  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.                    
  47.   
  48.                 // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了    
  49.                 // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着    
  50.                 if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {    
  51.                     String[] pkgList = appProcessInfo.pkgList;    
  52.                     for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名    
  53.                         String appName = null;  
  54.                         try {  
  55.                             appName = (String) pm.getApplicationLabel(pm.getApplicationInfo(pkgList[j], 0));   
  56.                         } catch (NameNotFoundException e) {  
  57.                             // TODO Auto-generated catch block  
  58.                             e.printStackTrace();  
  59.                         }  
  60.                         Log.d(TAG, "It will be killed, package name : " + pkgList[j]+" -- "+appName );    
  61.                         am.killBackgroundProcesses(pkgList[j]);    
  62.                         count++;    
  63.                     }    
  64.                 }    
  65.   
  66.             }    
  67.         }    
  68.   
  69.         long afterMem = getAvailMemory(this);    
  70.         Log.d(TAG, "----------- after memory info : " + afterMem);    
  71.         Toast.makeText(this"clear " + count + " process, "    
  72.                     + (afterMem - beforeMem) + "M", Toast.LENGTH_LONG).show();    
  73.     }  
  74.     private long getAvailMemory(CleanProcessActivity cleanProcessActivity) {  
  75.         // 获取android当前可用内存大小    
  76.         ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);    
  77.         MemoryInfo mi = new MemoryInfo();    
  78.         am.getMemoryInfo(mi);    
  79.         //mi.availMem; 当前系统的可用内存    
  80.         //return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化    
  81.         Log.d(TAG, "可用内存---->>>" + mi.availMem / (1024 * 1024));    
  82.         return mi.availMem / (1024 * 1024);    
  83.     }   
  84. }  


注意:

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

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

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

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

猜你喜欢

转载自blog.csdn.net/lzpdz/article/details/54646404