Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

android - How do I get the CPU temperature?

I know it's somehow possible to get the CPU's temperature because I downloaded an app that does it in an unrooted device. How is it done?

edit: The application is called A1 CPU Tool. I spent several days searching for an answer.

edit2: Here's the code I tried

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    Sensor TempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
    mSensorManager.registerListener(temperatureSensor, TempSensor, SensorManager.SENSOR_DELAY_FASTEST);
}

private SensorEventListener temperatureSensor = new SensorEventListener(){

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float temp = event.values[0];
        Log.i("sensor", "sensor temp = " + temp);
    }
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read the file from following paths (Since it is different for different devices) to get cpu temperature details from different devices, One of the path will return the needed file.

    "/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp"             
    "/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp"    
    "/sys/class/thermal/thermal_zone1/temp"                      
    "/sys/class/i2c-adapter/i2c-4/4-004c/temperature"            
    "/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/temperature" 
    "/sys/devices/platform/omap/omap_temp_sensor.0/temperature"  
    "/sys/devices/platform/tegra_tmon/temp1_input"               
    "/sys/kernel/debug/tegra_thermal/temp_tj"                   
    "/sys/devices/platform/s5p-tmu/temperature"                  
    "/sys/class/thermal/thermal_zone0/temp"                      
    "/sys/devices/virtual/thermal/thermal_zone0/temp"            
    "/sys/class/hwmon/hwmon0/device/temp1_input"                 
    "/sys/devices/virtual/thermal/thermal_zone1/temp"            
    "/sys/devices/platform/s5p-tmu/curr_temp"    

To read the file, I used:

RandomAccessFile reader = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp", "r");
String line = reader.readLine();

Here is the another way: Read system files


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...