获得android手机的CPU核心数

本文介绍如何在Android设备上获取CPU核心数量、频率等信息,并提供内存、存储空间及系统版本等参数的读取方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/stephen255/article/details/9056361
//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;
    }
}


不需要root权限。

/sys/devices/system/cpu/cpu0 ----------Cpu1

/sys/devices/system/cpu/cpu1 ----------Cpu2

获取CPU最大频率

     // "/system/bin/cat" 命令行
     // "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径

  1. public static long getCpuFrequence()  
  2.         ProcessBuilder cmd;  
  3.         try  
  4.             String[] args "/system/bin/cat",  
  5.                     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };  
  6.             cmd new ProcessBuilder(args);  
  7.   
  8.             Process process cmd.start();  
  9.             BufferedReader reader new BufferedReader(new InputStreamReader(  
  10.                     process.getInputStream()));  
  11.             String line reader.readLine();  
  12.             return StringUtils.parseLongSafe(line, 10, 0);  
  13.         catch (IOException ex)  
  14.             ex.printStackTrace();  
  15.          
  16.         return 0;  
  17.      
public static long getCpuFrequence() {
                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();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(
                                        process.getInputStream()));
                        String line = reader.readLine();
                        return StringUtils.parseLongSafe(line, 10, 0);
                } catch (IOException ex) {
                        ex.printStackTrace();
                }
                return 0;
        }

// 获取CPU最小频率(单位KHZ):
  1. public static String getMinCpuFreq()  
  2.             String result "";  
  3.             ProcessBuilder cmd;  
  4.             try  
  5.                     String[] args "/system/bin/cat",  
  6.                                     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };  
  7.                     cmd new ProcessBuilder(args);  
  8.                     Process process cmd.start();  
  9.                     InputStream in process.getInputStream();  
  10.                     byte[] re new byte[24];  
  11.                     while (in.read(re) != -1)  
  12.                             result result new String(re);  
  13.                      
  14.                     in.close();  
  15.             catch (IOException ex)  
  16.                     ex.printStackTrace();  
  17.                     result "N/A";  
  18.              
  19.             return result.trim();  
  20.      
    public static String getMinCpuFreq() {
                String result = "";
                ProcessBuilder cmd;
                try {
                        String[] args = { "/system/bin/cat",
                                        "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_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();
        }

// 实时获取CPU当前频率(单位KHZ):
  1. public static String getCurCpuFreq()  
  2.               String result "N/A";  
  3.               try  
  4.                       FileReader fr new FileReader(  
  5.                                       "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");  
  6.                       BufferedReader br new BufferedReader(fr);  
  7.                       String text br.readLine();  
  8.                       result text.trim();  
  9.               catch (FileNotFoundException e)  
  10.                       e.printStackTrace();  
  11.               catch (IOException e)  
  12.                       e.printStackTrace();  
  13.                
  14.               return result;  
  15.        
  public static String getCurCpuFreq() {
                String result = "N/A";
                try {
                        FileReader fr = new FileReader(
                                        "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
                        BufferedReader br = new BufferedReader(fr);
                        String text = br.readLine();
                        result = text.trim();
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return result;
        }

/ 获取CPU名字:
  1. public static String getCpuName()  
  2.              try  
  3.                      FileReader fr new FileReader("/proc/cpuinfo");  
  4.                      BufferedReader br new BufferedReader(fr);  
  5.                      String text br.readLine();  
  6.                      String[] array text.split(":\\s+", 2);  
  7.                      for (int 0; array.length; i++)  
  8.                       
  9.                      return array[1];  
  10.              catch (FileNotFoundException e)  
  11.                      e.printStackTrace();  
  12.              catch (IOException e)  
  13.                      e.printStackTrace();  
  14.               
  15.              return null;  
  16.       
   public 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 (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return null;
        }

内存:/proc/meminfo:
  1. public void getTotalMemory()    
  2.         String str1 "/proc/meminfo";    
  3.         String str2="";    
  4.         try    
  5.             FileReader fr new FileReader(str1);    
  6.             BufferedReader localBufferedReader new BufferedReader(fr, 8192);    
  7.             while ((str2 localBufferedReader.readLine()) != null)    
  8.                 Log.i(TAG, "---" str2);    
  9.                
  10.         catch (IOException e)    
  11.            
  12.       
public void getTotalMemory() {  
        String str1 = "/proc/meminfo";  
        String str2="";  
        try {  
            FileReader fr = new FileReader(str1);  
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);  
            while ((str2 = localBufferedReader.readLine()) != null) {  
                Log.i(TAG, "---" + str2);  
            }  
        } catch (IOException e) {  
        }  
    } 

Rom大小  www.2cto.com
  1. public long[] getRomMemroy()    
  2.         long[] romInfo new long[2];    
  3.         //Total rom memory     
  4.         romInfo[0] getTotalInternalMemorySize();    
  5.      
  6.         //Available rom memory     
  7.         File path Environment.getDataDirectory();    
  8.         StatFs stat new StatFs(path.getPath());    
  9.         long blockSize stat.getBlockSize();    
  10.         long availableBlocks stat.getAvailableBlocks();    
  11.         romInfo[1] blockSize availableBlocks;    
  12.         getVersion();    
  13.         return romInfo;    
  14.        
  15.      
  16.     public long getTotalInternalMemorySize()    
  17.         File path Environment.getDataDirectory();    
  18.         StatFs stat new StatFs(path.getPath());    
  19.         long blockSize stat.getBlockSize();    
  20.         long totalBlocks stat.getBlockCount();    
  21.         return totalBlocks blockSize;    
  22.       
public long[] getRomMemroy() {  
        long[] romInfo = new long[2];  
        //Total rom memory  
        romInfo[0] = getTotalInternalMemorySize();  
   
        //Available rom memory  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long availableBlocks = stat.getAvailableBlocks();  
        romInfo[1] = blockSize * availableBlocks;  
        getVersion();  
        return romInfo;  
    }  
   
    public long getTotalInternalMemorySize() {  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long totalBlocks = stat.getBlockCount();  
        return totalBlocks * blockSize;  
    } 

sdCard大小:
  1. public long[] getSDCardMemory()    
  2.         long[] sdCardInfo=new long[2];    
  3.         String state Environment.getExternalStorageState();    
  4.         if (Environment.MEDIA_MOUNTED.equals(state))    
  5.             File sdcardDir Environment.getExternalStorageDirectory();    
  6.             StatFs sf new StatFs(sdcardDir.getPath());    
  7.             long bSize sf.getBlockSize();    
  8.             long bCount sf.getBlockCount();    
  9.             long availBlocks sf.getAvailableBlocks();    
  10.      
  11.             sdCardInfo[0] bSize bCount;//总大小     
  12.             sdCardInfo[1] bSize availBlocks;//可用大小     
  13.            
  14.         return sdCardInfo;    
  15.       
public long[] getSDCardMemory() {  
        long[] sdCardInfo=new long[2];  
        String state = Environment.getExternalStorageState();  
        if (Environment.MEDIA_MOUNTED.equals(state)) {  
            File sdcardDir = Environment.getExternalStorageDirectory();  
            StatFs sf = new StatFs(sdcardDir.getPath());  
            long bSize = sf.getBlockSize();  
            long bCount = sf.getBlockCount();  
            long availBlocks = sf.getAvailableBlocks();  
   
            sdCardInfo[0] = bSize * bCount;//总大小  
            sdCardInfo[1] = bSize * availBlocks;//可用大小  
        }  
        return sdCardInfo;  
    } 

系统的版本信息:
  1. public String[] getVersion(){    
  2.     String[] version={"null","null","null","null"};    
  3.     String str1 "/proc/version";    
  4.     String str2;    
  5.     String[] arrayOfString;    
  6.     try    
  7.         FileReader localFileReader new FileReader(str1);    
  8.         BufferedReader localBufferedReader new BufferedReader(    
  9.                 localFileReader, 8192);    
  10.         str2 localBufferedReader.readLine();    
  11.         arrayOfString str2.split("\\s+");    
  12.         version[0]=arrayOfString[2];//KernelVersion     
  13.         localBufferedReader.close();    
  14.     catch (IOException e)    
  15.        
  16.     version[1] Build.VERSION.RELEASE;// firmware version     
  17.     version[2]=Build.MODEL;//model     
  18.     version[3]=Build.DISPLAY;//system version     
  19.     return version;    
  20.   
public String[] getVersion(){  
    String[] version={"null","null","null","null"};  
    String str1 = "/proc/version";  
    String str2;  
    String[] arrayOfString;  
    try {  
        FileReader localFileReader = new FileReader(str1);  
        BufferedReader localBufferedReader = new BufferedReader(  
                localFileReader, 8192);  
        str2 = localBufferedReader.readLine();  
        arrayOfString = str2.split("\\s+");  
        version[0]=arrayOfString[2];//KernelVersion  
        localBufferedReader.close();  
    } catch (IOException e) {  
    }  
    version[1] = Build.VERSION.RELEASE;// firmware version  
    version[2]=Build.MODEL;//model  
    version[3]=Build.DISPLAY;//system version  
    return version;  
} 

mac地址和开机时间
  1. public String[] getOtherInfo(){    
  2.     String[] other={"null","null"};    
  3.        WifiManager wifiManager (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);    
  4.        WifiInfo wifiInfo wifiManager.getConnectionInfo();    
  5.        if(wifiInfo.getMacAddress()!=null){    
  6.         other[0]=wifiInfo.getMacAddress();    
  7.     else    
  8.         other[0] "Fail";    
  9.        
  10.     other[1] getTimes();    
  11.        return other;    
  12.    
  13. private String getTimes()    
  14.     long ut SystemClock.elapsedRealtime() 1000;    
  15.     if (ut == 0)    
  16.         ut 1;    
  17.        
  18.     int (int) ((ut 60) 60);    
  19.     int (int) ((ut 3600));    
  20.     return mContext.getString(R.string.info_times_hour)   
  21.             mContext.getString(R.string.info_times_minute);    
  22.   
public String[] getOtherInfo(){  
    String[] other={"null","null"};  
       WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);  
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
       if(wifiInfo.getMacAddress()!=null){  
        other[0]=wifiInfo.getMacAddress();  
    } else {  
        other[0] = "Fail";  
    }  
    other[1] = getTimes();  
       return other;  
}  
private String getTimes() {  
    long ut = SystemClock.elapsedRealtime() / 1000;  
    if (ut == 0) {  
        ut = 1;  
    }  
    int m = (int) ((ut / 60) % 60);  
    int h = (int) ((ut / 3600));  
    return h + " " + mContext.getString(R.string.info_times_hour) + m + " " 
            + mContext.getString(R.string.info_times_minute);  
} 
---------------------------------------------------
  1. private static void updateCpuStat()  
  2.     ProcessBuilder cmd;  
  3.     long oldMypidStat first mypidStat;  
  4.     long oldTotal first total;  
  5.   
  6.     try  
  7.         StringBuilder builder new StringBuilder();  
  8.         int pid android.os.Process.myPid();  
  9.         String[] args "/system/bin/cat", "/proc/" pid "/stat" };  
  10.         cmd new ProcessBuilder(args);  
  11.   
  12.         Process process cmd.start();  
  13.         InputStream in process.getInputStream();  
  14.         byte[] re new byte[1024];  
  15.         int len;  
  16.         while ((len in.read(re)) != -1)  
  17.             builder.append(new String(re, 0, len));  
  18.          
  19.         // Log.i(TAG, builder.toString());   
  20.         in.close();  
  21.   
  22.         String[] stats builder.toString().split(" +", -1);  
  23.         if (stats.length >= 17)  
  24.             long utime StringUtils.parseLongSafe(stats[13], 10);  
  25.             long stime StringUtils.parseLongSafe(stats[14], 10);  
  26.             long cutime StringUtils.parseLongSafe(stats[15], 10);  
  27.             long cstime StringUtils.parseLongSafe(stats[16], 10);  
  28.             // Log.i(TAG, String.format(   
  29.             // "utime:%d, stime:%d, cutime:%d, cstime:%d", utime,   
  30.             // stime, cutime, cstime));   
  31.             mypidStat utime stime cutime cstime;  
  32.          
  33.     catch (IOException ex)  
  34.         ex.printStackTrace();  
  35.      
  36.   
  37.     try  
  38.         String[] args "/system/bin/cat", "/proc/stat" };  
  39.         cmd new ProcessBuilder(args);  
  40.   
  41.         Process process cmd.start();  
  42.         BufferedReader reader new BufferedReader(new InputStreamReader(  
  43.                 process.getInputStream()));  
  44.         String line null;  
  45.         while ((line reader.readLine()) != null)  
  46.             Matcher matcher pattern.matcher(line);  
  47.             if (matcher.matches())  
  48.                 String[] stats line.split(" +", -1);  
  49.                 long tmpTotal 0;  
  50.                 for (int 1; stats.length; i++)  
  51.                     tmpTotal += StringUtils.parseLongSafe(stats[i], 10);  
  52.                  
  53.                 total tmpTotal;  
  54.              
  55.             break;  
  56.          
  57.         if (first)  
  58.             first false;  
  59.         else  
  60.             Log.i(TAG, String.format("vsir cpu usage: %2.1f%%",  
  61.                     (double) (mypidStat oldMypidStat)  
  62.                             (total oldTotal) 100));  
  63.          
  64.         reader.close();  
  65.     catch (IOException ex)  
  66.         ex.printStackTrace();  
  67.      
  68. }  
内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值