Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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..

like image 892
samuelez Avatar asked Jan 15 '16 09:01

samuelez


People also ask

Is there an app to check CPU temp?

HWMonitor is one of the most trusted laptop temperature monitoring apps currently available.

What is CPU profiling?

CPU Profiler shows what functions consume what percent of CPU time This information can provide you a better understanding of how your application is executed, and how exactly resources are allocated. Once the analysis is finished, the profiler visualizes the output data in the re.


2 Answers

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

like image 94
OWADVL Avatar answered Sep 28 '22 06:09

OWADVL


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...

like image 25
hrk_er Avatar answered Sep 28 '22 05:09

hrk_er