上犹电脑信息网我们一直在努力
您的位置:上犹电脑信息网 > 设置问题 > 怎么看cpu温度-Android如何实现获取手机CPU的温度

怎么看cpu温度-Android如何实现获取手机CPU的温度

作者:上犹日期:

返回目录:设置问题

在做项目过程中,有时需要获取手机CPU的温度。


目前市面上常见的CPU主要有两种:MTK(联发科)、Qualcomm(高通)。当然还有我们华为的海思麒麟CPU,以及三星的CPU。后两种CPU在本篇文章中就不做展开,有兴趣的同学,可以自行去研究研究。


通过研究发现,CPU的信息基本都是在/sys/class/thermal/目录下,通过adb shell、cat相关命令可以拿到一些手机的CPU信息,基本步骤如下:


  1. 打开终端命令窗口,如windows下的cmd程序。
  2. 输入adb shell,回车。
  3. 输入cat /sys/class/thermal/thermal_zone7/type,回车。其中7只是一个示例,不同的手机可能会有区别。此命令可以获取相关的硬件类别。
  4. 输入cat /sys/class/thermal/thermal_zone7/temp,回车。就可以获取对应的温度值。

至此,可能MTK、Qualcomm的CPU怎么区分呢?


这里有一个小窍门:MTK的CPU名称类似为mtktscpu,Qualcomm的CPU名称类似为tsens_tz_sensor



说了这么多,那在Android程序里如何去实现呢?


重点来了,准备好了吗?


public static String getCpuTemp() {


String temp = "Unknow";


BufferedReader br = null;


FileReader fr = null;


try {


File dir = new File("/sys/class/thermal/");


File[] files = dir.listFiles(new FileFilter() {


@Override


public boolean accept(File file) {


if (Pattern.matches("thermal_zone[0-9]+", file.getName())) {


return true;


}


return false;


}


});


final int SIZE = files.length;


String line = "";


String type = "";


for (int i = 0; i < SIZE; i++) {


fr = new FileReader("/sys/class/thermal/thermal_zone" + i + "/type");


br = new BufferedReader(fr);


line = br.readLine();


if (line != null) {


type = line;


}


fr = new FileReader("/sys/class/thermal/thermal_zone" + i + "/temp");


br = new BufferedReader(fr);


line = br.readLine();


if (line != null) {


// MTK CPU


if (type.contains("cpu")) {


long temperature = Long.parseLong(line);


if (temperature < 0) {


temp = "Unknow";


} else {


temp = (float) (temperature / 1000.0) + "";


}


} else if (type.contains("tsens_tz_sensor")) {


// Qualcomm CPU


long temperature = Long.parseLong(line);


if (temperature < 0) {


temp = "Unknow";


} else if (temperature > 100){


temp = (float) (temperature / 10.0) + "";


} else {


temp = temperature + "";


}


}


}


}


if (fr != null) {


fr.close();


}


if (br != null) {


br.close();


}


} catch (Exception e) {


LogUtil.e(e);


} finally {


if (fr != null) {


try {


fr.close();


} catch (Exception e) {


LogUtil.e(e);


}


}


if (br != null) {


try {


br.close();


} catch (Exception e) {


LogUtil.e(e);


}


}


}


return temp;


}


这里只是提供一个基本的思路,大家可以在此基础上进行完善和扩展。


本文标签:怎么看cpu温度(27)

相关阅读

关键词不能为空
极力推荐

电脑蓝屏_电脑怎么了_win7问题_win10问题_设置问题_文件问题_上犹电脑信息网

关于我们