admin管理员组

文章数量:1530518

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

[plain]  view plain

copy

/**

* 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;

需要权限:

[html]  view plain

copy

具体操作代码如下:

[java]  view plain

copy

packagecom.example.demo;

importjava.util.List;

importandroid.app.Activity;

importandroid.app.ActivityManager;

importandroid.app.ActivityManager.MemoryInfo;

importandroid.app.ActivityManager.RunningAppProcessInfo;

importandroid.content.Context;

importandroid.content.pm.PackageManager;

importandroid.content.pm.PackageManager.NameNotFoundException;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.Menu;

importandroid.view.MenuItem;

importandroid.view.View;

importandroid.widget.Toast;

publicclassCleanProcessActivityextendsActivity {

privatestaticfinalString TAG ="Clean";

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_clean_process);

}

publicvoidclean(View v){

//To change body of implemented methods use File | Settings | File Templates.

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

List infoList = am.getRunningAppProcesses();

List serviceInfos = am.getRunningServices(100);

longbeforeMem = getAvailMemory(this);

Log.d(TAG, "-----------before memory info : "+ beforeMem);

intcount =0;

PackageManager pm = getPackageManager();

if(infoList !=null) {

for(inti =0; i 

RunningAppProcessInfo appProcessInfo = infoList.get(i);

Log.d(TAG, "process name : "+ appProcessInfo.processName);

//importance 该进程的重要程度  分为几个级别,数值越低就越重要。

Log.d(TAG, "importance : "+ appProcessInfo.importance);

// 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了

// 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着

if(appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {

String[] pkgList = appProcessInfo.pkgList;

for(intj =0; j 

String appName = null;

try{

appName = (String) pm.getApplicationLabel(pm.getApplicationInfo(pkgList[j], 0));

} catch(NameNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Log.d(TAG, "It will be killed, package name : "+ pkgList[j]+" -- "+appName );

am.killBackgroundProcesses(pkgList[j]);

count++;

}

}

}

}

longafterMem = getAvailMemory(this);

Log.d(TAG, "----------- after memory info : "+ afterMem);

Toast.makeText(this,"clear "+ count +" process, "

+ (afterMem - beforeMem) + "M", Toast.LENGTH_LONG).show();

}

privatelonggetAvailMemory(CleanProcessActivity cleanProcessActivity) {

// 获取android当前可用内存大小

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

MemoryInfo mi = newMemoryInfo();

am.getMemoryInfo(mi);

//mi.availMem; 当前系统的可用内存

//return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化

Log.d(TAG, "可用内存---->>>"+ mi.availMem / (1024*1024));

returnmi.availMem / (1024*1024);

}

}

注意:

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

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

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

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

小奋斗文章

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

本文标签: 一键内存功能android