传感器类型TYPE_TEMPERATURE已被弃用[自Android 2.3以来]可能提供有关CPU温度的信息.现在我们有传感器类型TYPE_AMBIENT_TEMPERATURE,它将为我们提供室温(我没有使用,&btw并非所有设备和/ Android版本都支持它)
我检查了几个测量CPU温度的应用程序.可能他们正在阅读系统文件.我尝试定位,在某些设备中我能够在以下路径中找到它:
sys/devices/virtual/thermal/thermal_zone0/temp
Run Code Online (Sandbox Code Playgroud)
结构位因供应商和测量单位而异.精细!但在许多设备中,我根本无法找到任何此类文件,并且在同一设备中这些应用程序可以正常工作!我想知道,怎么样!
我们如何测量Android中的CPU温度?
我正在使用此代码获取当前的CPU温度:
只见它太
private float getCurrentCPUTemperature() {
String file = readFile("/sys/devices/virtual/thermal/thermal_zone0/temp", '\n');
if (file != null) {
return Long.parseLong(file);
} else {
return Long.parseLong(batteryTemp + " " + (char) 0x00B0 + "C");
}
}
private byte[] mBuffer = new byte[4096];
@SuppressLint("NewApi")
private String readFile(String file, char endChar) {
// Permit disk reads here, as /proc/meminfo isn't really "on
// disk" and should be fast. TODO: make BlockGuard ignore
// /proc/ and /sys/ files perhaps?
StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
FileInputStream …Run Code Online (Sandbox Code Playgroud)