android检测cup温度工具,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;

}

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

欢迎大家各抒己见,O(∩_∩)O哈哈~

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值