android系统开发之优雅的杀后台

android系统开发之优雅的杀后台

    public void clean(){  
        //To change body of implemented methods use File | Settings | File Templates.    
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);    
        List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();    
        List<ActivityManager.RunningServiceInfo> serviceInfos = 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_SERVICE) {    
                    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() {  
        // 获取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);    
    } 
    public float getFreeMemory(){
		float memory = 0;
		MemoryInfo info = new MemoryInfo();
		mActivityManager.getMemoryInfo(info);
		memory = info.availMem/1024/1024;
		return memory;
	}
	public float getLocalMemory(){
		float localMemory = 0;
		String str1 = "/proc/meminfo";
		String str2;
		String[] arrayOfString;
		long initial_memory = 0;
		try {
			FileReader fileReader = new FileReader(str1);
			BufferedReader bufferedReader = new BufferedReader(fileReader, 8192);
			str2 = bufferedReader.readLine();
			arrayOfString = str2.split("\\s+");
			initial_memory = Integer.valueOf(arrayOfString[1]);
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		localMemory = (initial_memory / 1024);
		return localMemory;
}
	public int getPercentMemory(){
		int memoryPercent = 0;
		memoryPercent = (int) ((1-getFreeMemory()/getLocalMemory())*100);
		if(memoryPercent < 0 || memoryPercent >100){
		Log.e(TAG,"getPercentMemory Error memoryPercent == "+memoryPercent+" getFreeMemory() =="+getFreeMemory()+" getLocalMemory() == "+getLocalMemory());
		memoryPercent = 0; 
	}
		return memoryPercent;
	}

IMPORTANCE_FOREGROUND,
IMPORTANCE_FOREGROUND_SERVICE,
IMPORTANCE_TOP_SLEEPING,
IMPORTANCE_VISIBLE,
IMPORTANCE_PERCEPTIBLE,
IMPORTANCE_CANT_SAVE_STATE,
IMPORTANCE_SERVICE,
IMPORTANCE_CACHED,
IMPORTANCE_GONE,
具体意思:ActivityManager.RunningAppProcessInfo

参考:https://blog.csdn.net/lzpdz/article/details/54646404?utm_source=blogxgwz4

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坂田民工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值