private IThermalService getIThermalService() {
if (mThermalService == null) {
mThermalService = IThermalService.Stub.asInterface(
ServiceManager.getService("thermalservice"));
if (mThermalService != null) {
try {
mThermalService.asBinder().linkToDeath(() -> {
mThermalService = null;
}, /* flags */ 0);
} catch (RemoteException e) {
Slog.e(TAG, "linkToDeath with thermalService failed", e);
mThermalService = null;
}
}
}
return mThermalService;
}
//第一种 耗时3ms
//temp.getType() 3 , temp.getName() SKIN ,(int) (temp.getValue() * 10) = 384
//temp.getType() 0 , temp.getName() CPU ,(int) (temp.getValue() * 10) = 705
//temp.getType() 2 , temp.getName() BATTERY ,(int) (temp.getValue() * 10) = 329
public synchronized int getPcbTemperature() throws RemoteException {
Slog.e(TAG, "getPcbTemperature enter");
IThermalService thermalService = getIThermalService();
if (thermalService != null) {
Temperature temperatures[] = thermalService.getCurrentTemperatures();
for (Temperature temp : temperatures) {
if (temp.getName().equals("SKIN")) {
break; // 找到后退出循环
}
Slog.e(TAG, " temp.getType() "+ temp.getType() + " , temp.getName() " + temp.getName() + " ,(int) (temp.getValue() * 10) = " + (int) (temp.getValue() * 10));
}
}
Slog.e(TAG, "getPcbTemperature end");
第二种耗时20ms
if (mHardwarePropertiesManager != null) {
float[] pcbTempArray = mHardwarePropertiesManager.getDeviceTemperatures(
HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN,
HardwarePropertiesManager.TEMPERATURE_CURRENT);
if (pcbTempArray != null && pcbTempArray.length >= 1) {
//logD("skinTemp: " + pcbTempArray[0]);
int pcbTemp = (int) (pcbTempArray[0] * 10);//384 格式
return pcbTemp;
}
}
return 0;
}
第三种读节点的方式:
/sys/class/thermal/thermal_zone%d/type
这种弊端是节点不是固定不变的,需要根据type找到对应的节点在读/sys/class/thermal/thermal_zoneX/temp 这个是对应温度值