• android获取硬件信息


    1.获取CPU型号

    private static String getCpuName(){
     try{
          FileReader fr = new FileReader("/proc/cpuinfo");
          BufferedReader br = new BufferedReader(fr);
          String text = br.readLine();
          String[] array = text.split(":\s+",2);
           for(int i = 0; i < array.length; i++){
           }
           return array[1];
      }catch(FileNotFoundExecption e){
        e.printStackTrace();
      }catch (IOException e){
         e.printStackTrace();
      }
       return null;
    }

    2.获取CPU核心数

    private int getNumCores() {
        //Private Class to display only CPU devices in the directory listing
        class CpuFilter implements FileFilter {
            @Override
            public boolean accept(File pathname) {
                //Check if filename is "cpu", followed by a single digit number
                if(Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }      
        }
    
        try {
            //Get directory containing CPU info
            File dir = new File("/sys/devices/system/cpu/");
            //Filter to only list the devices we care about
            File[] files = dir.listFiles(new CpuFilter());
            Log.d(TAG, "CPU Count: "+files.length);
            //Return the number of cores (virtual CPU devices)
            return files.length;
        } catch(Exception e) {
            //Print exception
            Log.d(TAG, "CPU Count: Failed.");
            e.printStackTrace();
            //Default to return 1 core
            return 1;
        }
    }

    3.获取CPU最大频率

    public static String getMinCpuFreq() {  
                String result = "";  
                ProcessBuilder cmd;  
                try {  
                        String[] args = { "/system/bin/cat",  
                                        "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };  
                        cmd = new ProcessBuilder(args);  
                        Process process = cmd.start();  
                        InputStream in = process.getInputStream();  
                        byte[] re = new byte[24];  
                        while (in.read(re) != -1) {  
                                result = result + new String(re);  
                        }  
                        in.close();  
                } catch (IOException ex) {  
                        ex.printStackTrace();  
                        result = "N/A";  
                }  
                return result.trim();  
        } 

    4.RAM内存大小

    private long getRamMemory(Context context){
            String str1 = "/proc/meminfo";// 系统内存信息文件
            String str2;
            String[] arrayOfString;
            long initial_memory = 0; 
    
            try
            {
                FileReader localFileReader = new FileReader(str1);
                BufferedReader localBufferedReader = new BufferedReader(
                localFileReader, 8192);
                str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
    
                arrayOfString = str2.split("\s+");
                for (String num : arrayOfString) {
                    Log.i(str2, num + "	");
                }
    
                initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
                localBufferedReader.close();
    
            } catch (IOException e) {
            }
            //return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB,内存大小规格化
            System.out.println("总运存--->>>"+initial_memory/(1024*1024));
            return initial_memory/(1024*1024);
        }

    5.ROM大小

    public long getTotalInternalMemorySize() {    
            File path = Environment.getDataDirectory();    
            StatFs stat = new StatFs(path.getPath());    
            long blockSize = stat.getBlockSize();    
            long totalBlocks = stat.getBlockCount();    
            return totalBlocks * blockSize;    
        }   
    6.获取屏幕分辨率
    private String getScreenResolution(){
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
       String strOpt = dm.widthPixels + " * " + dm.heightPixels;
       return strOpt;
    }


  • 相关阅读:
    MySQL DNS反查导致连接缓慢
    重写官方TodoList,对于初学react+redux的人来说,很有好处
    第一个博客试一试
    需求分析
    原来==的优先级比&高 (转自地址为http://blog.163.com/cynicly@126/blog/static/1206105820103893715302/的网易博客)
    E: 无法获得锁 /var/lib/apt/lists/lock open (11 Resource temporarily unavailable)
    我的博客园终于开通啦!
    (转自CSDN日新为道的专栏)printf的格式控制符,在CSDN看到的,转在这里
    qrc_image.cpp未找到
    QT打开保存的html文件无法显示图片
  • 原文地址:https://www.cnblogs.com/chengliu/p/4130635.html
Copyright © 2020-2023  润新知