android+内存清理+代码,最新版本:Android一键式清理,内存清理功能的实现
Android一鍵式清理,內(nèi)存清理功能的實(shí)山清理大師等均提供一鍵式清理和一鍵加速等功能。實(shí)際上,它們殺死了一些后臺(tái)進(jìn)程以達(dá)到釋放內(nèi)存的目的。
基本思想是列出所有正在運(yùn)行的進(jìn)程,檢查它們的重要值(RunningAppProcessInfo.importance,該值越大,進(jìn)程的重要性越低),可以設(shè)置一個(gè)閾值,如果過(guò)程大于閾值,則可以終止該過(guò)程。
該過(guò)程的重要性具有以下級(jí)別:
/**
* 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;
需要權(quán)限:
具體操作代碼如下:
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 該進(jìn)程的重要程度 分為幾個(gè)級(jí)別,數(shù)值越低就越重要。
Log.d(TAG, "importance : " + appProcessInfo.importance);
// 一般數(shù)值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的進(jìn)程都長(zhǎng)時(shí)間沒(méi)用或者空進(jìn)程了
// 一般數(shù)值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的進(jìn)程都是非可見(jiàn)進(jìn)程,也就是在后臺(tái)運(yùn)行著
if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
String[] pkgList = appProcessInfo.pkgList;
for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到該進(jìn)程下運(yùn)行的包名
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當(dāng)前可用內(nèi)存大小
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
//mi.availMem; 當(dāng)前系統(tǒng)的可用內(nèi)存
//return Formatter.formatFileSize(context, mi.availMem);// 將獲取的內(nèi)存大小規(guī)格化
Log.d(TAG, "可用內(nèi)存---->>>" + mi.availMem / (1024 * 1024));
return mi.availMem / (1024 * 1024);
}
}
注意:
我在此處選擇的閾值為IMPORTANCE_VISIBLE級(jí)別,這意味著不可見(jiàn)的后臺(tái)進(jìn)程和服務(wù)將被殺死(某些系統(tǒng)進(jìn)程除外)。
清潔效果類(lèi)似于Kingsoft Cleaner和360 Desktop的一鍵式清潔效果。
如果您不想太猛烈地殺死進(jìn)程,則可以選擇IMPORTANCE_SERVICE級(jí)別來(lái)殺死那些長(zhǎng)時(shí)間沒(méi)有用或空的進(jìn)程。此級(jí)別的清理功能不足以實(shí)山清理大師的效果。
以上是本文的全部?jī)?nèi)容。希望對(duì)每個(gè)人的學(xué)習(xí)都有幫助,也希望您能為腳本庫(kù)提供更多支持。
本文來(lái)自電腦雜談,轉(zhuǎn)載請(qǐng)注明本文網(wǎng)址:
http://www.pc-fly.com/a/shoujiruanjian/article-326460-1.html
總結(jié)
以上是生活随笔為你收集整理的android+内存清理+代码,最新版本:Android一键式清理,内存清理功能的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机组成原理哈工大期末_浅谈计算机组成
- 下一篇: rfid阅读器的主要任务_RFID阅读器