繁体   English   中英

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

[英]How I get CPU usage and temperature information into an android app?

我正在开发一个应用程序,我不知道如何获得CPU使用率和温度,例如在textview中。 我尝试使用TYPE_AMBIENT_TEMPERATURE来获取温度,但它不起作用。 日志说“我没有传感器”,但通过在游戏商店中使用其他应用程序,我得到温度和频率..

如果我使用TYPE_GYROSCOPE等其他传感器,代码工作正常,所以我不明白TYPE_AMBIENT_TEMPERATURE如何工作..

抱歉我的英语不好......请帮帮我..

您可以使用的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://stackoverflow.com/a/11931903/1031297

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) {
 }

PS。 首先需要检查传感器是否存在......如果不存在,则无法做任何事情。 我猜一些应用程序谎言。

PS2。 您可以随时对应用程序进行反向工程,以查看它们如何显示温度;)

这是因为你的设备根本没有温度计。 许多旧设备没有它。 Android文档称设备“MAY”内置了这些传感器,但并非“必须”拥有这些传感器。

显示温度的其他应用程序是通过自己的方法计算的(可能在某些范围内都有所不同)

我目前正在制作自己的应用程序来测量CPU温度...

正如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上找到了解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM