android系统进程手机内存工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class TaskUtils { 
   
    /**
     * 获取正在运行的进程的个数
     
     * @param context
     * @return
     */ 
    public static int getRunningProcessCount(Context context) { 
        // 得到系统的任务管理器. 
        ActivityManager am = (ActivityManager) context 
                .getSystemService(Context.ACTIVITY_SERVICE); 
        return am.getRunningAppProcesses().size(); 
    
   
    /**
     * 获取手机的可用内存
     
     * @param context
     * @return long类型数据 总的可用内存
     */ 
    public static long getAvailRam(Context context) { 
        ActivityManager am = (ActivityManager) context 
                .getSystemService(Context.ACTIVITY_SERVICE); 
        ActivityManager.MemoryInfo outInfo = new MemoryInfo(); 
        am.getMemoryInfo(outInfo); 
        return outInfo.availMem; 
    
   
    /**
     * 获取手机的总内存
     
     * @return
     */ 
    public static long getTotalRam() { 
        try
            File file = new File("/proc/meminfo"); 
            FileInputStream fis = new FileInputStream(file); 
            BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 
            // MemTotal: 253604 kB 
            String result = br.readLine(); 
            StringBuffer sb = new StringBuffer(); 
            char[] chars = result.toCharArray(); 
            for (char c : chars) { 
                if (c >= '0' && c <= '9') { 
                    sb.append(c); 
                
            
            return Long.parseLong(sb.toString()) * 1024
        } catch (Exception e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            return 0
   
        
    
}

编程技巧