如何以编程方式获取 android cpu 温度
how to get android cpu temperature programmatically
试过了,但得到的是 0.0,但在物理设备上什么也找不到。
在 android
中获得 cpu 温度的任何方法
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);
}
private final SensorEventListener AmbientTemperatureSensorListener
= 新的 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));
}
}
};
这种东西有系统服务
HardwarePropertiesManager 包含一个方法 getDeviceTemperatures(int type, int source)
,可从 Nougat
获得
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
HardwarePropertiesManager hardwarePropertiesManager= (HardwarePropertiesManager) getSystemService(Context.HARDWARE_PROPERTIES_SERVICE);
float[] temp = hardwarePropertiesManager.getDeviceTemperatures(HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU, HardwarePropertiesManager.TEMPERATURE_CURRENT);
}
看看这个:
https://developer.android.com/reference/android/os/HardwarePropertiesManager
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;
}
}
您可以从此代码中找到所有热值(温度和类型)(不仅是 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;
}
Logcat 中的示例输出(下图是实际设备输出...在模拟器上它只显示类型 battery及其温度。):
试过了,但得到的是 0.0,但在物理设备上什么也找不到。 在 android
中获得 cpu 温度的任何方法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);
}
private final SensorEventListener AmbientTemperatureSensorListener = 新的 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));
}
}
};
这种东西有系统服务
HardwarePropertiesManager 包含一个方法 getDeviceTemperatures(int type, int source)
,可从 Nougat
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
HardwarePropertiesManager hardwarePropertiesManager= (HardwarePropertiesManager) getSystemService(Context.HARDWARE_PROPERTIES_SERVICE);
float[] temp = hardwarePropertiesManager.getDeviceTemperatures(HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU, HardwarePropertiesManager.TEMPERATURE_CURRENT);
}
看看这个: https://developer.android.com/reference/android/os/HardwarePropertiesManager
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;
}
}
您可以从此代码中找到所有热值(温度和类型)(不仅是 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;
}
Logcat 中的示例输出(下图是实际设备输出...在模拟器上它只显示类型 battery及其温度。):