Android 如何获得CPU温度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20771070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the CPU temperature?
提问by Asaf
I know it's somehow possible to get the CPU's temperature because I downloaded an app that does it in an unrooted device. How is it done?
我知道有可能以某种方式获得 CPU 的温度,因为我下载了一个在无根设备中执行此操作的应用程序。它是如何完成的?
edit: The application is called A1 CPU Tool. I spent several days searching for an answer.
编辑:该应用程序称为 A1 CPU 工具。我花了几天时间寻找答案。
edit2: Here's the code I tried
编辑2:这是我试过的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor TempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
mSensorManager.registerListener(temperatureSensor, TempSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
private SensorEventListener temperatureSensor = new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float temp = event.values[0];
Log.i("sensor", "sensor temp = " + temp);
}
};
回答by Jyoti JK
Read the file from following paths (Since it is different for different devices) to get cpu temperature details from different devices, One of the path will return the needed file.
从以下路径读取文件(因为不同设备不同)以获取不同设备的cpu温度详细信息,其中一个路径将返回所需的文件。
"/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp"
"/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp"
"/sys/class/thermal/thermal_zone1/temp"
"/sys/class/i2c-adapter/i2c-4/4-004c/temperature"
"/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/temperature"
"/sys/devices/platform/omap/omap_temp_sensor.0/temperature"
"/sys/devices/platform/tegra_tmon/temp1_input"
"/sys/kernel/debug/tegra_thermal/temp_tj"
"/sys/devices/platform/s5p-tmu/temperature"
"/sys/class/thermal/thermal_zone0/temp"
"/sys/devices/virtual/thermal/thermal_zone0/temp"
"/sys/class/hwmon/hwmon0/device/temp1_input"
"/sys/devices/virtual/thermal/thermal_zone1/temp"
"/sys/devices/platform/s5p-tmu/curr_temp"
To read the file, I used:
为了读取文件,我使用了:
RandomAccessFile reader = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp", "r");
String line = reader.readLine();
Here is the another way: Read system files
这是另一种方式:读取系统文件
回答by Matthew Henry
If a device doesn't expose CPU temperature via either temperature sensor (that is, asking for a sensor of type TEMPERATURE
or AMBIENT_TEMPERATURE
gives you nothing), you won't be able to read it in a standard manner. Of course, that says nothing about doing it in a non-standard way which might be suitable for your application if it's not going for wide distribution.
如果设备没有通过任一温度传感器暴露 CPU 温度(即,要求传感器类型TEMPERATURE
或不AMBIENT_TEMPERATURE
提供任何传感器),您将无法以标准方式读取它。当然,这并没有说明以非标准方式进行操作,如果它不适合广泛分发,则可能适合您的应用程序。
You can read off device files in /sys/devices/platform
if you know where to look. If you run adb shell
and cd
into that folder, you've got fairly good chances of find | grep temp
giving you files representing temperature sensors.
/sys/devices/platform
如果您知道在哪里查看,您可以读取设备文件。如果您运行adb shell
并cd
进入该文件夹,则很有可能find | grep temp
为您提供代表温度传感器的文件。
These files also aren't just going to be for the CPU only - as an example the battery temperature sensor for my Google Glass can be accessed by reading /sys/devices/platform/omap_i2c.1/i2c-1/1-0055/power_supply/bq27520-0/temp
.
这些文件也不仅仅适用于 CPU - 例如,可以通过读取/sys/devices/platform/omap_i2c.1/i2c-1/1-0055/power_supply/bq27520-0/temp
.
Looking at that path it's clear why you shouldn't do this and expect it to work on all devices - the paths and also the formats of the files will vary from device to device. However in a pinch it's a quick and dirty way to get the job done.
查看该路径很清楚为什么您不应该这样做并期望它可以在所有设备上运行 - 文件的路径和格式会因设备而异。然而,在紧要关头,这是完成工作的一种快速而肮脏的方式。
回答by Moheuddin Sehab
http://repo.xposed.info/module/org.narcotek.cputemp
http://repo.xposed.info/module/org.narcotek.cputemp
Read this one carefully, I think it might help you.
仔细阅读这一篇,我想它可能对你有帮助。
I am using an app called cool tool; it's amazing it has lots of features... Then I enabled custom labels 1 and followed what the link said.
I changed the path to /sys/class/thermal/thermal_zone0/temp
and there was this option which I didn't change refer:(/d+)/d{3}
And also replacement pattern:1$
Hope this helps use cool tool if you have to!
我正在使用一个名为cool tool 的应用程序;令人惊讶的是它有很多功能......然后我启用了自定义标签 1 并按照链接所说的进行操作。我改变了路径/sys/class/thermal/thermal_zone0/temp
,有一个我没有改变的选项,refer:(/d+)/d{3}
还有替换模式:1$ 希望这有助于使用很酷的工具,如果你需要的话!
回答by Cornelius Roemer
Here is a current set of paths that are used in production in Dec 2019 to find CPU temperature. There are so many different paths because different OEMs put the temperature in different places.
这是 2019 年 12 月在生产中使用的一组当前路径,用于查找 CPU 温度。有很多不同的路径,因为不同的 OEM 将温度放在不同的地方。
These three locations are also used in addition to the one listed above:
除了上面列出的位置之外,还使用了这三个位置:
/sys/htc/cpu_temp
/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/ext_temperature
/sys/devices/platform/tegra-tsensor/tsensor_temperature
The ones already listed as answer previously still hold true:
之前已经列为答案的那些仍然适用:
/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp
/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp
/sys/class/thermal/thermal_zone1/temp
/sys/class/i2c-adapter/i2c-4/4-004c/temperature
/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/temperature
/sys/devices/platform/omap/omap_temp_sensor.0/temperature
/sys/devices/platform/tegra_tmon/temp1_input
/sys/kernel/debug/tegra_thermal/temp_tj
/sys/devices/platform/s5p-tmu/temperature
/sys/class/thermal/thermal_zone0/temp
/sys/devices/virtual/thermal/thermal_zone0/temp
/sys/class/hwmon/hwmon0/device/temp1_input
/sys/devices/virtual/thermal/thermal_zone1/temp
/sys/devices/platform/s5p-tmu/curr_temp
回答by SASHI
The temperature sensor even if exists if for the battery temperature and CPU temperature.
温度传感器即使存在电池温度和CPU温度。
Most phones only include CPU temperature measurement sensor Sensor.TYPE_TEMPERATURE which is deprecated
大多数手机仅包含 CPU 温度测量传感器 Sensor.TYPE_TEMPERATURE,已弃用
You should use Sensor.TYPE_AMBIENT_TEMPERATURE
which I don't think many phones have.
你应该使用.TYPE_AMBIENT_TEMPERATURE
我认为很多手机都没有的传感器。
For More information please go through this
欲了解更多信息,请通过此
http://developer.android.com/reference/android/hardware/Sensor.html#TYPE_AMBIENT_TEMPERATURE
http://developer.android.com/reference/android/hardware/Sensor.html#TYPE_AMBIENT_TEMPERATURE