Zee*_*hdi 1 cpu android temperature
尝试了一下,但得到了0.0,并且在物理设备上找不到任何东西。
SensorManager mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor AmbientTemperatureSensor
= mySensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (AmbientTemperatureSensor != null) {
mySensorManager.registerListener(
AmbientTemperatureSensorListener,
AmbientTemperatureSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
Run Code Online (Sandbox Code Playgroud)
私有最终SensorEventListener AmbientTemperatureSensorListener = new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
temperature = event.values[0];
Messages.sendMessage(getApplicationContext(),Float.toString(temperature));
}
}
};
Run Code Online (Sandbox Code Playgroud)
您可以从此代码中找到所有热值(温度和类型)(不仅仅是 CPU 温度)。还要记住,sys/class/thermal/thermal_zone0/temp
并不总是指向 CPU 温度(在我的例子中,它指向电池温度)。始终在后台线程中使用此代码。我已经在真实设备和模拟器上对其进行了测试,并且运行良好。
public void thermal() {
String temp, type;
for (int i = 0; i < 29; i++) {
temp = thermalTemp(i);
if (!temp.contains("0.0")) {
type = thermalType(i);
if (type != null) {
System.out.println("ThermalValues "+type+" : "+temp+"\n");
}
}
}
}
public String thermalTemp(int i) {
Process process;
BufferedReader reader;
String line;
String t = null;
float temp = 0;
try {
process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone" + i + "/temp");
process.waitFor();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
line = reader.readLine();
if (line != null) {
temp = Float.parseFloat(line);
}
reader.close();
process.destroy();
if (!((int) temp == 0)) {
if ((int) temp > 10000) {
temp = temp / 1000;
} else if ((int) temp > 1000) {
temp = temp / 100;
} else if ((int) temp > 100) {
temp = temp / 10;
}
} else
t = "0.0";
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public String thermalType(int i) {
Process process;
BufferedReader reader;
String line, type = null;
try {
process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone" + i + "/type");
process.waitFor();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
line = reader.readLine();
if (line != null) {
type = line;
}
reader.close();
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
return type;
}
Run Code Online (Sandbox Code Playgroud)
Logcat 中的示例输出(下图是真实设备输出...在模拟器上仅显示电池类型及其温度。):
public static float cpuTemperature()
{
Process process;
try {
process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
if(line!=null) {
float temp = Float.parseFloat(line);
return temp / 1000.0f;
}else{
return 51.0f;
}
} catch (Exception e) {
e.printStackTrace();
return 0.0f;
}
}
Run Code Online (Sandbox Code Playgroud)