简体   繁体   中英

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

I'm developing an application and I don't know how can I get CPU usage and temperature, example in a textview. I try with TYPE_AMBIENT_TEMPERATURE to get temperature, but it doesn't work. Logs say that "i haven't the sensor", but by using other apps in the play store I get the temperature and freq..

The code works fine if I use other sensor like TYPE_GYROSCOPE or other, so I don't understand how TYPE_AMBIENT_TEMPERATURE doesn't work..

Sorry for my bad english...and please help me..

for the CPU frequency you can use

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;
}

you might want to use this one too:

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;
    }
}

for the temperature ( 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. You first need to check if the sensor is present...If it isn't, there's nothing that can be done. I guess some of the apps lie.

PS2. You can always reverse engineer an app to see how they display the temperature ;)

this is because your device simply doesn't have the thermometer. A lot of older devices don't have it. Android documentation says devices "MAY" have these sensors built in, but are not "required" to have them.

the other apps that are showing you the temperature, are calculating by their own methods (which may all be different to certain extents)

I am currently in the process of making my own app that measures CPU temperatures myself...

As OWADVL answered, you can ready the temperature also as a system file, like this:

int temperature = readSystemFileAsInt("sys/class/thermal/thermal_zone0/temp");

note that the readSystemFileAsInt is not a system call. I found the implementation here

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();
}

About the CPU usage (load) you can look at this question and already working solution by Souch at his gitHub here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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