android 清理程序,android 清理

先看下面有关android的设备信息,有关Memory量

# cat meminfo

MemTotal: 352088 kB

MemFree: 15728 kB

Buffers: 4232 kB

Cached: 103788 kB

SwapCached: 0 kB

Active: 224688 kB

Inactive: 78044 kB

Active(anon): 193220 kB

Inactive(anon): 2584 kB

Active(file): 31468 kB

Inactive(file): 75460 kB

Unevictable: 312 kB

Mlocked: 0 kB

HighTotal: 0 kB

HighFree: 0 kB

LowTotal: 352088 kB

LowFree: 15728 kB

SwapTotal: 0 kB

SwapFree: 0 kB

Dirty: 0 kB

Writeback: 0 kB

AnonPages: 195036 kB

Mapped: 43684 kB

Shmem: 780 kB

Slab: 7608 kB

SReclaimable: 2896 kB

SUnreclaim: 4712 kB

KernelStack: 3864 kB

PageTables: 12632 kB

NFS_Unstable: 0 kB

Bounce: 0 kB

WritebackTmp: 0 kB

CommitLimit: 176044 kB

Committed_AS: 1889040 kB

VmallocTotal: 483328 kB

VmallocUsed: 197648 kB

VmallocChunk: 260100 kB

dumpsys meminfo -a查看进程的内存使用情况

** MEMINFO in pid 2342 [com.pateo.VoiceRecognition] **

native dalvik other total

size: 16600 3271 N/A 19871

allocated: 16298 3014 N/A 19312

free: 233 257 N/A 490

(Pss): 4598 1147 17510 23255

(shared dirty): 1176 1804 2164 5144

(priv dirty): 4564 404 12384 17352

Objects

Views: 0 ViewRoots: 0

AppContexts: 0 Activities: 0

Assets: 18 AssetManagers: 18

Local Binders: 36 Proxy Binders: 52

Death Recipients: 6

OpenSSL Sockets: 0

下面代码主要为内存的获取接口调用:

public long readAvailMem() {

try {

long memFree = 0;

long memCached = 0;

FileInputStream is = new FileInputStream("/proc/meminfo");

int len = is.read(mBuffer);

is.close();

final int BUFLEN = mBuffer.length;

for (int i=0; i

if (matchText(mBuffer, i, "MemFree")) {

i += 7;

memFree = extractMemValue(mBuffer, i);

} else if (matchText(mBuffer, i, "Cached")) {

i += 6;

memCached = extractMemValue(mBuffer, i);

}

while (i < BUFLEN && mBuffer[i] != '\n') {

i++;

}

}

return memFree + memCached;

} catch (java.io.FileNotFoundException e) {

} catch (java.io.IOException e) {

}

return 0;

}

private boolean matchText(byte[] buffer, int index, String text) {

int N = text.length();

if ((index+N) >= buffer.length) {

return false;

}

for (int i=0; i

if (buffer[index+i] != text.charAt(i)) {

return false;

}

}

return true;

}

private long extractMemValue(byte[] buffer, int index) {

while (index < buffer.length && buffer[index] != '\n') {

if (buffer[index] >= '0' && buffer[index] <= '9') {

int start = index;

index++;

while (index < buffer.length && buffer[index] >= '0'

&& buffer[index] <= '9') {

index++;

}

String str = new String(buffer, 0, start, index-start);

return ((long)Integer.parseInt(str))* 1024;

}

index++;

}

return 0;

}

下面为清理:

public int autoCleanup(int killImportance) {

final int sdkVersion = android.os.Build.VERSION.SDK_INT;

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

final ArrayList blackList = blackListArray();

final ArrayList whiteList = whiteList();

List processes = am.getRunningAppProcesses();

int psize = processes.size();

int cleannum = 0;

for (int i = 0; i < psize; i++) {

String pkgName = processes.get(i).processName;

if(pkgName == null) {

continue;

}

if (DEBUG)

Log.d(TAG, "process name: " + pkgName + " " + processes.get(i).importance);

try {

PackageManager pm = mContext.getPackageManager();

ApplicationInfo info = pm.getApplicationInfo(pkgName,

PackageManager.GET_UNINSTALLED_PACKAGES

| PackageManager.GET_DISABLED_COMPONENTS);

if ((((processes.get(i).importance >= killImportance

&& ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) && !whiteList

.contains(pkgName))) || blackList.contains(pkgName)) &&!pkgName.equals(getTopActivy()) ) {

String playingPackage = getThirdPlayingPackageName();

if(playingPackage!=null){

if(pkgName.equals(playingPackage)){

continue;

}

}

if (sdkVersion >= 8) {

am.killBackgroundProcesses(pkgName);

} else {

am.restartPackage(pkgName);

}

cleannum++;

if (DEBUG)

Log.d(TAG,

"kill : " + pkgName + " "

+ processes.get(i).importance);

}

} catch (Exception e) {

Log.w(TAG, "Exception caught: " + e);

}

}

return cleannum;

}

public String getTopActivy(){

ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);

List runningTaskInfos = manager.getRunningTasks(1);

String cmpNameTemp = null;

if(null != runningTaskInfos){

cmpNameTemp=(runningTaskInfos.get(0).topActivity).getPackageName();

}

return cmpNameTemp;

}

其中上面的代码blackListArray存储的是黑名单,存储对象为应用包名,whiteList为白名单,getThirdPlayingPackageName为当前播放的多媒体不需要被杀为比较好的策略,

当然你还可以定义一些自己想要的策略

可以通过按键加广播接收方式,如写在一个应用里面然后把自己杀了

android.os.Process.killProcess(android.os.Process.myPid());

System.exit(0);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值