如何将CPU使用率和温度信息导入Android应用程序?

6
我正在开发一个应用程序,但不知道如何在TextView中获取CPU使用率和温度。我试着使用TYPE_AMBIENT_TEMPERATURE来获取温度,但它没有起作用。日志显示“我没有这个传感器”,但我在Play商店中使用其他应用程序可以获取温度和频率。如果我使用其他传感器(如TYPE_GYROSCOPE或其他),代码可以正常工作,所以我不明白为什么TYPE_AMBIENT_TEMPERATURE不能正常工作。对于我的糟糕英语,我很抱歉,请帮帮我。
3个回答

3

对于 CPU 频率,您可以使用以下命令:

public static int[] getCPUFrequencyCurrent() throws Exception {
    int[] output = new int[getNumCores()];
    for(int i=0;i<getNumCores();i++) {
        output[i] = readSystemFileAsInt("/sys/devices/system/cpu/cpu"+String.valueOf(i)+"/cpufreq/scaling_cur_freq");
    }
    return output;
}

你可能也想使用这个:
public static 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());
        //Return the number of cores (virtual CPU devices)
        return files.length;
    } catch(Exception e) {
        //Default to return 1 core
        return 1;
    }
}

关于温度的问题 (https://dev59.com/bmfWa4cB1Zd3GeqPezUO#11931903)

public class TempSensorActivity extends Activity, implements SensorEventListener {
 private final SensorManager mSensorManager;
 private final Sensor mTempSensor;

 public TempSensorActivity() {
     mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
     mTempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
 }

 protected void onResume() {
     super.onResume();
     mSensorManager.registerListener(this, mTempSensor, SensorManager.SENSOR_DELAY_NORMAL);
 }

 protected void onPause() {
     super.onPause();
     mSensorManager.unregisterListener(this);
 }

 public void onAccuracyChanged(Sensor sensor, int accuracy) {
 }

 public void onSensorChanged(SensorEvent event) {
 }

注意:首先需要检查传感器是否存在...如果不存在,则无法进行任何操作。我猜一些应用程序是虚假的。

注意2:您可以始终反向工程一个应用程序,以查看它们如何显示温度;)


2
正如OWADVL所回答的,你也可以像这样将温度读取为系统文件:
int temperature = readSystemFileAsInt("sys/class/thermal/thermal_zone0/temp");

请注意,readSystemFileAsInt不是系统调用。我在这里找到了实现。
private static int readSystemFileAsInt(final String pSystemFile) throws Exception {
    InputStream in = null;
    try {
        final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();

        in = process.getInputStream();
        final String content = readFully(in);
        return Integer.parseInt(content);
    } catch (final Exception e) {
        throw new Exception(e);
    }
}

private static final String readFully(final InputStream pInputStream) throws IOException {
    final StringBuilder sb = new StringBuilder();
    final Scanner sc = new Scanner(pInputStream);
    while(sc.hasNextLine()) {
        sb.append(sc.nextLine());
    }
    return sb.toString();
}

关于CPU使用率(负载),您可以查看此问题,以及Souch在他的gitHub这里上的已经可用的解决方案。

1
这是因为您的设备没有温度计。许多旧设备都没有它。 Android文档称设备“可能”内置这些传感器,但不“必须”拥有它们。
其他显示温度的应用程序是通过自己的方法进行计算的(这些方法在某种程度上可能都不同)。
我目前正在制作自己的应用程序来测量CPU温度...

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