android+内存清理+代码,最新版本:Android一键式清理,内存清理功能的实现

002f3dc4193b335eb9f11fe4298bccd8.png

Android一键式清理,内存清理功能的实山清理大师等均提供一键式清理和一键加速等功能。实际上,它们杀死了一些后台进程以达到释放内存的目的。

基本思想是列出所有正在运行的进程,检查它们的重要值(RunningAppProcessInfo.importance,该值越大,进程的重要性越低),可以设置一个阈值,如果过程大于阈值,则可以终止该过程。

该过程的重要性具有以下级别:

e67f068dc13e83a4012dc1fcd654c695.png

/**

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

需要权限:

669ff9de9463e2460d282d03e58985d0.png

具体操作代码如下:

package com.example.demo;

import java.util.List;

import android.app.Activity;

import android.app.ActivityManager;

import android.app.ActivityManager.MemoryInfo;

import android.app.ActivityManager.RunningAppProcessInfo;

import android.content.Context;

import android.content.pm.PackageManager;

import android.content.pm.PackageManager.NameNotFoundException;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Toast;

public class CleanProcessActivity extends Activity {

private static final String TAG = "Clean";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_clean_process);

}

public void clean(View v){

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

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

ListinfoList = am.getRunningAppProcesses();

ListserviceInfos = am.getRunningServices(100);

long beforeMem = getAvailMemory(this);

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

int count = 0;

PackageManager pm = getPackageManager();

if (infoList != null) {

for (int i = 0; i < infoList.size(); ++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 (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名

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

}

}

}

}

long afterMem = getAvailMemory(this);

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

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

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

}

private long getAvailMemory(CleanProcessActivity cleanProcessActivity) {

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

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

MemoryInfo mi = new MemoryInfo();

am.getMemoryInfo(mi);

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

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

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

return mi.availMem / (1024 * 1024);

}

}

注意:

1e8e879ba33f02b6e8dede4030eaf4e5.png

我在此处选择的阈值为IMPORTANCE_VISIBLE级别,这意味着不可见的后台进程和服务将被杀死(某些系统进程除外)。

清洁效果类似于Kingsoft Cleaner和360 Desktop的一键式清洁效果。

如果您不想太猛烈地杀死进程,则可以选择IMPORTANCE_SERVICE级别来杀死那些长时间没有用或空的进程。此级别的清理功能不足以实山清理大师的效果。

以上是本文的全部内容。希望对每个人的学习都有帮助,也希望您能为脚本库提供更多支持。

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/shoujiruanjian/article-326460-1.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/503516.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android xml通知栏权限配置,Android开发中 AndroidManifest.xml配置之service,receiver标签配置详解...

本文主要来分享service,receiver标签配置。如有错误&#xff0c;欢迎指正。android:directBootAware["true" | "false"]android:enabled["true" | "false"]android:exported["true" | "false"]android:foreground…

cups共享linux打印机_linux入门-映射网络驱动器

linux入门-映射网络驱动器在日常中&#xff0c;我们不会时时刻刻远程着linux服务器&#xff0c;那么有没有办法可以让我们在window电脑上映射linux的磁盘呢&#xff1f;这是可以实现的&#xff0c;这里我们就要介绍samba了。sambaSamba是在Linux和UNIX系统上实现SMB协议的一个免…

c++ idea 插件_idea快速开发插件

插件&#xff1a;1、Background Image Plus这款插件并不能直接提高你的开发效率&#xff0c;但是可以让你面对的IDE不再单调&#xff0c;当把背景设置成你自己心仪的的图片&#xff0c;是不是会感觉很赏心悦目&#xff0c;编码效率会不会因此间接的提高&#xff1f;&#xff01…

android 删除模拟器,android – 如何从avd设备中删除脱机模拟器?

我在Android Studio中创建了几个AVD-s.他们在模拟器端口5554上启动.然后我通过android avd应用程序创建了另一个AVD,他们从端口5556开始.现在运行一个模拟器,我从adb devices -l获得以下输出&#xff1a;List of devices attachedemulator-5556 deviceemulator-5554 offline我正…

mongodb 搜索速度_MongoDB数据库查询性能提高40倍的经历分享

前言数据库性能对软件整体性能有着至关重要的影响&#xff0c;本文给大家分享了一次MongoDB数据库查询性能提高40倍的经历&#xff0c;感兴趣的朋友们可以参考学习。背景说明1、数据库&#xff1a;MongoDB2、数据集&#xff1a;A&#xff1a;字段数不定&#xff0c;这里主要用到…

signature=8405d26e250ad07c44560263cb1d4fc0,Systems for analyzing microtissue arrays

摘要&#xff1a;A tissue microarray imaging system autonomously images, analyzes, and stores data for samples in a tissue microarray. The system may include a tissue microarray, a robotic microscope, and an imaging workstation that executes software to aut…

ios支付宝支付失败不回调_为什么 iOS 支付成功后能回到 APP ,但是没有回调?...

接入客户端从服务器端拿到 charge 对象后&#xff0c;调用下面的方法[Pingpp createPayment:chargeviewController:viewControllerappURLScheme:kUrlSchemewithCompletion:^(NSString *result, PingppError *error) {if ([result isEqualToString:"success"]) { …

html如何将设置文本效果,css如何对文本进行修饰

color属性&#xff1a;设置文本文字颜色。用法如下&#xff1a;color&#xff1a;颜色值;color属性可以设置的合法颜色值包括&#xff1a;16进制颜色值(例&#xff1a;#ffffff)&#xff0c;rgb颜色值【例&#xff1a;rgb(0,0,0)】&#xff0c;rgba颜色值【例&#xff1a;rgb(0,…

HTML与cgi post传递与接收,CGI实例--表单GET与POST示例

CGI概述CGI(Common Gateway Interface: 公用网关接口)规定了Web服务器调用其他可执行程序(CGI程 序)的接口协议标准。Web服务器通过调用CGI程序实现和Web浏览器的交互, 也就是CGI程序通过读标准输入&#xff0c;接受Web浏览器发送给Web服务器的信息, 进行处理, 将响应结果再通过…

vue横向树结构_vue树形结构的实现

1. 主要代码使用单文件组件的方式, 需要一个父组件treeMenu, 和子组件treeItem1.1 父组件treeMenu.vue:nodes"treeData">export default {name: treeMenu,data: () > {return {treeData: {label: china,nodes: [{label: hubei,nodes: [{label: wuhan},{label: …

cesium js 路径_vue2.0项目集成Cesium的实现方法

安装cesium在已有项目中执行,npm i cesium修改配置build/webpack.base.conf.js1、定义 Cesium 源码路径const cesiumSource ../node_modules/cesium/Sourceuse strictconst path require(path)const utils require(./utils)const config require(../config)const vueLoader…

pytorch模型加载测试_pytorch模型加载方法汇总

Pytorch有很多方便易用的包&#xff0c;今天要谈的是torchvision包&#xff0c;它包括3个子包&#xff0c;分别是&#xff1a; torchvison.datasets &#xff0c;torchvision.models &#xff0c;torchvision.transforms &#xff0c;分别是预定义好的数据集(比如MNIST、CIFAR1…

android studio聊天跳转_Android 第三方应用跳转到QQ进行聊天

跳转QQ聊天代码十分简单&#xff1a;//获取包信息public static booleanisQQClientAvailable(Context context) {finalPackageManager packageManager context.getPackageManager();List pinfo packageManager.getInstalledPackages(0);if(pinfo !null) {for(inti 0;i < p…

html字体闪烁模板,CSS+JS阴影闪烁文字

阴影闪烁文字.F1 {filter: glow(Color#FF8000,Strength10);width150px;height200px;}.F2 {filter: glow(Color#00FF00,Strength9);width110px;height200px;}.F3 {filter: glow(Color#0080FF,Strength12);width90px;height200px;}var rate 500var i 0var F F1function doThin…

html 规定换行,HTML 换行

css3整理--clipclip语法: .selector { clip: rect | auto | inherit } 注意:clip属性只能在元素设置了“position:absolute”或者“position:fi ...Android 边框圆角RelativeLayout 圆角实现: drawable目录下面定义shape的xml文件: mall_header_rel_bg.xml <?xml version&…

qt执行命令行失败_QT缺少 qtcore4.dll,debug下运行不成功

刚装QT的时候&#xff0c;好像我的环境变量没有设置好&#xff0c;哎&#xff0c;。隐患终于爆发了。在VS下运行成功的QT程序&#xff0c;然后点击Debug下的.exe&#xff0c;老提示缺少Qtcore4.dll&#xff0c;当时正郁闷之极&#xff0c;忘了怎样在网页上查找&#xff0c;只是…

伪类如何动态在html设置样式,用js实现before和after伪类的样式修改的示例代码

本文介绍了使用javascript,jQuery实现修改before,after伪类的样式&#xff0c;分享给大家&#xff0c;具体如下&#xff1a;最近遇到一个需要改变:before,:after 伪类的样式&#xff0c;发现css中并不能直接选择某一个元素的:before和:after伪类元素&#xff0c;所以特总结了使…

html 图片平移动画,CSS3 圆圈内图片的自动平移/旋转动画

CSS语言&#xff1a;CSSSCSS确定body {background: #fbfbfb;}.spinner {width: 155px;height: 155px;border-radius: 100%;background-color: #d8d8d8;border: 10px solid #575757;position: absolute;top: 50%;left: 50%;transform: translateX(-50%) translateY(-50%) transl…

layui option 动态添加_layui select动态添加option的实例

html产品类别轻松融容易融快乐融增加产品类别js//重新渲染表单function renderForm(){layui.use(form, function(){var form layui.form();//高版本建议把括号去掉&#xff0c;有的低版本&#xff0c;需要加()form.render();});}//增加产品类别按钮点击事件function addProduc…

html鼠标滑轮换图片,JavaScript实现鼠标滚轮控制页面图片切换

鼠标上的滚轮是一个不错的东东&#xff0c;为什么这么说&#xff0c;因为它能帮助我们快速的浏览网页,快速的进行长篇文章的阅读。对于web前端的我们来说又怎么能不注重这个鼠标滚轮呢&#xff0c;那么它能如何让用户更好的浏览网页呢&#xff1f;本文主要介绍JavaScript实现鼠…