Android 一键清理、内存清理功能实现
360桌面、金山清理大师等都提供了一键清理、一键加速等功能,其实就是杀一些后台进程来达到释放内存的目的。
基本思路就是列出所有运行的进程,查看其重要值(RunningAppProcessInfo.importance,值越大说明进程重要程度越低),可以设定一个阈值,如果该进程的重要值大于该阈值,就可以杀掉该进程。
进程的重要值有以下几个等级:
/** *Constantfor{@link#importance}:thisisapersistentprocess. *Onlyusedwhenreportingtoprocessobservers. *@hide */ publicstaticfinalintIMPORTANCE_PERSISTENT=50; /** *Constantfor{@link#importance}:thisprocessisrunningthe *foregroundUI. */ publicstaticfinalintIMPORTANCE_FOREGROUND=100; /** *Constantfor{@link#importance}:thisprocessisrunningsomething *thatisactivelyvisibletotheuser,thoughnotintheimmediate *foreground. */ publicstaticfinalintIMPORTANCE_VISIBLE=200; /** *Constantfor{@link#importance}:thisprocessisrunningsomething *thatisconsideredtobeactivelyperceptibletotheuser.An *examplewouldbeanapplicationperformingbackgroundmusicplayback. */ publicstaticfinalintIMPORTANCE_PERCEPTIBLE=130; /** *Constantfor{@link#importance}:thisprocessisrunningan *applicationthatcannotsaveitsstate,andthuscan'tbekilled *whileinthebackground. *@hide */ publicstaticfinalintIMPORTANCE_CANT_SAVE_STATE=170; /** *Constantfor{@link#importance}:thisprocessiscontainsservices *thatshouldremainrunning. */ publicstaticfinalintIMPORTANCE_SERVICE=300; /** *Constantfor{@link#importance}:thisprocessprocesscontains *backgroundcodethatisexpendable. */ publicstaticfinalintIMPORTANCE_BACKGROUND=400; /** *Constantfor{@link#importance}:thisprocessisemptyofany *activelyrunningcode. */ publicstaticfinalintIMPORTANCE_EMPTY=500;
需要权限:
<uses-permissionandroid:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
具体操作代码如下:
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{ privatestaticfinalStringTAG="Clean"; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_clean_process); } publicvoidclean(Viewv){ //TochangebodyofimplementedmethodsuseFile|Settings|FileTemplates. ActivityManageram=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo>infoList=am.getRunningAppProcesses(); List<ActivityManager.RunningServiceInfo>serviceInfos=am.getRunningServices(100); longbeforeMem=getAvailMemory(this); Log.d(TAG,"-----------beforememoryinfo:"+beforeMem); intcount=0; PackageManagerpm=getPackageManager(); if(infoList!=null){ for(inti=0;i<infoList.size();++i){ RunningAppProcessInfoappProcessInfo=infoList.get(i); Log.d(TAG,"processname:"+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<pkgList.length;++j){//pkgList得到该进程下运行的包名 StringappName=null; try{ appName=(String)pm.getApplicationLabel(pm.getApplicationInfo(pkgList[j],0)); }catch(NameNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } Log.d(TAG,"Itwillbekilled,packagename:"+pkgList[j]+"--"+appName); am.killBackgroundProcesses(pkgList[j]); count++; } } } } longafterMem=getAvailMemory(this); Log.d(TAG,"-----------aftermemoryinfo:"+afterMem); Toast.makeText(this,"clear"+count+"process," +(afterMem-beforeMem)+"M",Toast.LENGTH_LONG).show(); } privatelonggetAvailMemory(CleanProcessActivitycleanProcessActivity){ //获取android当前可用内存大小 ActivityManageram=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); MemoryInfomi=newMemoryInfo(); am.getMemoryInfo(mi); //mi.availMem;当前系统的可用内存 //returnFormatter.formatFileSize(context,mi.availMem);//将获取的内存大小规格化 Log.d(TAG,"可用内存---->>>"+mi.availMem/(1024*1024)); returnmi.availMem/(1024*1024); } }
注意:
我这里选择阈值是IMPORTANCE_VISIBLE级别的,也就是非可见的后台进程和服务会被杀掉(一些系统进程肯定除外)。
清理的效果跟金山清理大师和360桌面的一键清理效果差不多。
如果不想杀的太凶,可以选择IMPORTANCE_SERVICE级别,杀掉那些长时间没用或者空进程了,这个级别的清理力度不够大,达不到金山清理大师的效果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。