android一键清理任务,Android 一键清理、内存清理功能实现
基本思路就是列出所有运行的进程,查看其重要值(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一键清理任务,Android 一键清理、内存清理功能实现相关推荐
- microsoftfixitoffice官方清理软件_最好用内存清理软件之一,45元永久会员官方限时送!速度上车!...
关于手机清理软件,发哥以前推荐过不少,安卓端最著名的清理工具就是ES文件浏览器(点击跳转查看),这款工具不仅有着优秀的文件清理.管理功能,还支持度盘不限速下载,目前的最新解锁版已经更新了,获取方式文末 ...
- C++写一个内存清理软件(三、内存清理)
一.界面 添加static.check.list.button 二.重绘 重绘控件,改变控件颜色 右键类向导,生成函数后加入以下代码 三.初始化 右键类向导,在生成函数中加入以下代码 四.交互 双击清 ...
- 测试大白菜软件,大白菜memtest内存清理测试工具的简介
我们都知道电脑用久了,系统内存会随着程序的运行而增加垃圾文件,从而影响到电脑性能,虽然很多朋友想着要清理电脑的系统内存,但又苦于不知从何下手,今天就给大家介绍下使用u深度memtest内存清理工具来进 ...
- android+内存清理+代码,最新版本:Android一键式清理,内存清理功能的实现
Android一键式清理,内存清理功能的实山清理大师等均提供一键式清理和一键加速等功能.实际上,它们杀死了一些后台进程以达到释放内存的目的. 基本思想是列出所有正在运行的进程,检查它们的重要值(Run ...
- android一键清理后台,Android 一键清理、内存清理功能实现
360桌面.金山清理大师等都提供了一键清理.一键加速等功能,其实就是杀一些后台进程来达到释放内存的目的. 基本思路就是列出所有运行的进程,查看其重要值(RunningAppProcessInfo.im ...
- android获取进程内存使用信息、一键加速(内存清理)与进程重要级别解析
android获取进程内存使用信息.一键加速(内存清理)与进程重要级别解析 获取进程内存使用信息 获取单个或多个进程 获取系统内存状态的信息 MemoryInfo说明 内存清理 killBackgro ...
- Android开发实战《手机安全卫士》——10.“进程管理”模块实现 PopupWindow 内存清理
文章目录 1.软件管理--PopupWindow的使用 2.软件管理--PopupWindow的动画 3.软件管理--卸载应用 & 启动应用 & 分享应用 4.进程管理--布局实现 5 ...
- android 内存清理
欢迎转载,转载请注明:http://blog.csdn.net/zhgxhuaa 说明 在上一篇中介绍了"垃圾清理",在系统优化中有一个功能往往是与垃圾清理分不开的,那就是&quo ...
- Android踩内存工具,手机内存清理软件下载
软件标签: 内存清理 主要功能 1.收集系统正在运行任务的详细信息 2.自定义内存清理模式,使系统运行畅快自如 3.进程切换,软件卸载一键完成 安卓手机清理内存到底有什么作用? 1.手机反应速度变快 ...
- android代码实现手机加速功能,Android自定义View实现内存清理加速球效果
Android自定义View实现内存清理加速球效果 发布时间:2020-09-21 22:21:57 来源:脚本之家 阅读:105 作者:程序员的自我反思 前言 用过猎豹清理大师或者相类似的安全软件, ...
最新文章
- java null布局_Java Swing 绝对布局管理方法,null布局
- Winforn中DevExpress的TreeList中显示某路径下的所有目录和文件(附源码下载)
- 小议Oracle外键约束修改行为
- 1042 字符统计 (20 分)(c语言)
- 如何保证代码的高质量?
- 云栖大会展出两款一体机,搭载新一代无影融合架构
- 原生 Ajax 封装 和 Axios 二次 封装
- 元数据(MetaData)
- 花了三个月终于把所有的 Python 库全部整理了!可以说很全面了
- Shiro - RememberMe记住我功能实现
- 图像质量评价(IQA)综述
- catic备份mysql,caticbj.com
- Word文档怎么翻译?翻译word文档简单步骤讲解
- Python中的多线程是假的多线程
- Android之友盟多渠道打包与数据统计(非原创)
- 新颖的自我介绍_简单新颖的自我介绍范文
- 网络原理之TCP/UDP协议
- SLI、SLO和SLA
- 2022年山东省安全员C证考试题及在线模拟考试
- MarsEdit for Mac(博客写作软件)