• (二十三)进程清理的方法


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

      

  • 相关阅读:
    第二十九课 循环链表的实现
    第二十八课 再论智能指针(下)
    第二十七课 再论智能指针(上)
    第二十六课 典型问题分析(Bugfix)
    普通new和placement new的重载
    leetcode 581. Shortest Unsorted Continuous Subarray
    leetcode 605. Can Place Flowers
    leetcode 219. Contains Duplicate II
    leetcode 283. Move Zeroes
    leetcode 217. Contains Duplicate
  • 原文地址:https://www.cnblogs.com/fuyanan/p/4137350.html
Copyright © 2020-2023  润新知