如何从“/proc/cpuinfo”文件中获取安卓设备的特定信息?

3

我该如何解析我的Android平板电脑的/proc/cpuinfo虚拟文件以获取处理器核心和时钟速度的信息?我不需要上述文件提供的所有信息,只需要这两个。能否有人帮忙吗?

2个回答

8
  • It is not clear if you want this information inside your app, or just for your own use.

    you can get this information on with adb:

    adb shell cat /proc/cpuinfo
    

    adb shell cpuinfo

  • If you want to use this information in your app, create a simple function to return a Map<String,String>, for example,

    public static Map<String, String> getCpuInfoMap() {
        Map<String, String> map = new HashMap<String, String>();
        try {
            Scanner s = new Scanner(new File("/proc/cpuinfo"));
            while (s.hasNextLine()) {
                String[] vals = s.nextLine().split(": ");
                if (vals.length > 1) map.put(vals[0].trim(), vals[1].trim());
            }
        } catch (Exception e) {Log.e("getCpuInfoMap",Log.getStackTraceString(e));}
        return map;
    }
    

    Note, this will not get multiple cpus information, overwrites. Most of the values are similar anyways. or Modify to create List of CpuInfoMaps.

    try,

    Log.d("getCpuInfoMap test", getCpuInfoMap().toString());
    

我想在我的应用程序中获取这两个信息。但不是所有的信息,抱歉!这就是为什么我只写了这两个值的原因。 - user3897419
2
仅从映射中获取这两个值...例如:getCpuInfoMap().get("cpu MHz") - ashoke
谢谢,但我在那里找不到我想要的值,即“处理器核心和时钟速度”。 - user3897419
1
@android_bitter 发布你的 adb shell cat /proc/cpuinfo 我可以帮你提取出你想要的数值。 - ashoke
我正在获取地图值这些 "{CPU implementer=0x41, Serial=444155163231473411000138b1880707, CPU architecture=7, Hardware=sun6i, CPU revision=3, CPU variant=0x0, CPU part=0xc07, Revision=000b, BogoMIPS=2013.59, processor=3, Features=swp half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpv4 idiva idivt, Processor=ARMv7 Processor rev 3 (v7l)}",但没有我要找的值。但是这里有一些值 /proc/stat。 - user3897419
1
@android_bitter 在我的情况下,我能够获取一些设备的序列号,但对于某些设备,它给出的是值。你有什么想法我错过了什么吗? - rupesh

1
我希望现在回答不算太晚,以下是我获取特定CPU核心当前频率的方法:
public class MainActivity extends Activity{

private static final int INSERTION_POINT = 27;

private static String getCurFrequencyFilePath(int whichCpuCore){
    StringBuilder filePath = new StringBuilder("/sys/devices/system/cpu/cpu/cpufreq/scaling_cur_freq");
    filePath.insert(INSERTION_POINT, whichCpuCore);
    return filePath.toString();
 }

public static int getCurrentFrequency(int whichCpuCore){

     int curFrequency = -1;
     String cpuCoreCurFreqFilePath = getCurFrequencyFilePath(whichCpuCore);

     if(new File(cpuCoreCurFreqFilePath).exists()){

         try {
                BufferedReader br = new BufferedReader(new FileReader(new File(cpuCoreCurFreqFilePath)));
                String aLine;
                while ((aLine = br.readLine()) != null) {

                    try{
                        curFrequency = Integer.parseInt(aLine);
                    }
                    catch(NumberFormatException e){

                        Log.e(getPackageName(), e.toString());
                    }

                }
                if (br != null) {
                    br.close();
                }
        } 
        catch (IOException e) {
            Log.e(getPackageName(), e.toString());
        }

     }

     return curFrequency;
 }

}

从这里开始,这很容易,您只需调用该方法:-D
int core1CurrentFreq = getCurrentFrequency(1, this); 

有时候核心会离线,这种情况下文件路径不存在,会返回-1。
注意:返回值为KHz,MHz的值为core1CurrentFreq / 1e3,GHz的值为core1CurrentFreq / 1e6。
关于getCurFrequencyFilePath()方法的一些解释,因为它并不是那么清晰。
当前频率通常存储在文件scaling_cur_freq中。
文件路径如下:
"/sys/devices/system/cpu/cpu(XX)/cpufreq/scaling_cur_freq"

其中(XX)将被替换为CPU核心数,例如:
"/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"

INSERTION_POINT变量实际上是(XX)的索引,我们希望在该处放置与CPU核心相应的数字。
我建议您查看cpufreq文件夹中的其他文件,您可以使用它们获取其他信息,例如最大和最小频率、可用频率列表等等。
点击此处链接并向下滚动至标题3。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接