zoukankan      html  css  js  c++  java
  • (二十三)进程清理的方法

    	/**
    	 * Android将进程分为6个等级,它们按优先级顺序由高到低依次是: 1) 前台进程( FOREGROUND_APP) 2)
    	 * 可视进程(VISIBLE_APP ) 3) 次要服务进程(SECONDARY_SERVER ) 4) 后台进程 (HIDDEN_APP) 5)
    	 * 内容供应节点(CONTENT_PROVIDER) 6) 空进程(EMPTY_APP)。注意:注意值越大说明进程重要程度越低。
    	 */
    	public void ClearMemoryAction() {
    		// final String TAG = "clearMemory";
    		ActivityManager am = (ActivityManager) context
    				.getSystemService(Context.ACTIVITY_SERVICE);
    		List<RunningAppProcessInfo> runnningAppProcessInfo = am
    				.getRunningAppProcesses(); // 获得正在运行的进程
    		if (runnningAppProcessInfo != null) {
    			for (int i = 0; i < runnningAppProcessInfo.size(); ++i) {
    				RunningAppProcessInfo appProcessInfo = runnningAppProcessInfo
    						.get(i);
    
    				// 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
    				if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
    					String[] packagesList = appProcessInfo.pkgList; // 运行在该进程下的所有应用程序包名,一个进程里可以运行一个或多个应用程序
    					for (int j = 0; j < packagesList.length; ++j) {
    						am.killBackgroundProcesses(packagesList[j]);
    						Log.i(TAG, "杀死的进程的包名" + packagesList[j]);
    					}
    				}
    			}
    		}
    
    	}
    
    	public int getProcessCount() {
    		// final String TAG = "clearMemory";
    
    		ActivityManager am = (ActivityManager) context
    				.getSystemService(Context.ACTIVITY_SERVICE);
    		List<RunningAppProcessInfo> runnningAppProcessInfo = am
    				.getRunningAppProcesses(); // 获得正在运行的进程
    		int count = 0;
    		if (runnningAppProcessInfo != null) {
    			for (int i = 0; i < runnningAppProcessInfo.size(); ++i) {
    				RunningAppProcessInfo appProcessInfo = runnningAppProcessInfo
    						.get(i);
    				// 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
    				if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
    					String[] packagesList = appProcessInfo.pkgList; // 运行在该进程下的所有应用程序包名,一个进程里可以运行一个或多个应用程序
    					for (int j = 0; j < packagesList.length; ++j) {
    						count++;
    					}
    				}
    			}
    		}
    		Log.i(TAG, "杀死的进程的数目" + count);
    		return count;
    	}
    

      

  • 相关阅读:
    Python 学习笔记 11.模块(Module)
    Python 学习笔记 8.引用(Reference)
    Python 学习笔记 9.函数(Function)
    Python 学习笔记 6.List和Tuple
    Python 学习笔记 4.if 表达式
    Python 学习笔记 2.自省
    Python 学习笔记 3.简单类型
    Python 学习笔记 7.Dictionary
    Python 学习笔记 5.对象驻留
    Python 学习笔记 10.类(Class)
  • 原文地址:https://www.cnblogs.com/fuyanan/p/4137350.html
Copyright © 2011-2022 走看看