zoukankan      html  css  js  c++  java
  • android项目得到进程数量,运行内存和剩余运行内存

    package com.itheima.utils;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    
    import android.app.ActivityManager;
    import android.app.ActivityManager.MemoryInfo;
    import android.content.Context;
    import android.text.format.Formatter;
    
    public class RunRomUtils {
        //得到运行的程序
        public static int getCountRom(Context context){
            ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
            return am.getRunningAppProcesses().size();
            
        }
        //得到剩余运行内存空间
        public static String getAvail(Context context){
            ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
            MemoryInfo outInfo = new MemoryInfo();
            am.getMemoryInfo(outInfo );
            
            return Formatter.formatFileSize(context, outInfo.availMem);
        }
        //得到总运行内存空间
        public static String getTotal(Context context){
            //4.1.2及以上可以用这个办法
    //        ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
    //        MemoryInfo outInfo = new MemoryInfo();
    //        am.getMemoryInfo(outInfo );
    //        return Formatter.formatFileSize(context, outInfo.totalMem);
            File file = new File("/proc/meminfo");
            FileInputStream fileIn;
            try {
                fileIn = new FileInputStream(file);
                InputStreamReader in = new InputStreamReader(fileIn);
                BufferedReader br = new BufferedReader(in);
                String result = br.readLine();
                String total = "";
                StringBuffer sb = new StringBuffer();
                for (char c : result.toCharArray()) {
                    if(c>='0'&&c<='9'){
                        sb.append(c);
                    }
                }
                int c = Integer.valueOf(sb.toString())*1024;
                return Formatter.formatFileSize(context,c);
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "";
            }
        }
    }
  • 相关阅读:
    记一次file_get_contents报failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request的错
    记一次centos7下配置服务器的过程
    locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录
    VM12虚拟机Centos7配置动态IP的网络设置
    记录下防御SSH爆破攻击的经验(CentOS7.3)
    第6次实践作业 17组
    第5次实践作业
    第3次实践作业
    第2次实践作业
    第1次实践作业
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5040172.html
Copyright © 2011-2022 走看看