@@ -202,9 +202,52 @@ private void updateCPUInfoView() {
202202 int avgClockSpeed = totalClockSpeed / clockSpeeds .length ;
203203 TextView tvCPUTitle = findViewById (R .id .TVCPUTitle );
204204 byte cpuUsagePercent = (byte ) (((float ) avgClockSpeed / maxClockSpeed ) * 100.0f );
205- tvCPUTitle .setText ("CPU (" + cpuUsagePercent + "%)" );
205+
206+ // 获取CPU温度并添加到标题
207+ float cpuTemp = getCpuTemperature ();
208+ String tempText = cpuTemp > 0 ? String .format (Locale .getDefault (), " %.1f°C" , cpuTemp ) : "" ;
209+ tvCPUTitle .setText ("CPU (" + cpuUsagePercent + "%)" + tempText );
206210 }
207211
212+ private float getCpuTemperature () {
213+ try {
214+ // 方法1: 通过 ThermalManager API (API 29+)
215+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
216+ ThermalManager thermalManager = activity .getSystemService (ThermalManager .class );
217+ if (thermalManager != null ) {
218+ List <ThermalInfo > thermalInfos = thermalManager .getCurrentThermals ();
219+ for (ThermalInfo info : thermalInfos ) {
220+ if (info .getType () == ThermalInfo .TYPE_CPU ) {
221+ return info .getTemperature ();
222+ }
223+ }
224+ }
225+ }
226+
227+ // 方法2: 通过读取系统文件 (不需要root)
228+ String [] possiblePaths = {
229+ "/sys/class/thermal/thermal_zone0/temp" ,
230+ "/sys/devices/virtual/thermal/thermal_zone0/temp" ,
231+ "/sys/class/hwmon/hwmon0/temp1_input"
232+ };
233+
234+ for (String path : possiblePaths ) {
235+ try (RandomAccessFile file = new RandomAccessFile (path , "r" )) {
236+ String content = file .readLine ();
237+ if (content != null ) {
238+ float temp = Float .parseFloat (content .trim ());
239+ return temp > 1000 ? temp / 1000 : temp ; // 转换为摄氏度
240+ }
241+ } catch (Exception e ) {
242+ // 忽略错误,尝试下一个路径
243+ }
244+ }
245+ } catch (Exception e ) {
246+ e .printStackTrace ();
247+ }
248+ return -1 ; // 无法获取温度
249+ }
250+
208251 private void updateMemoryInfoView () {
209252 ActivityManager activityManager = (ActivityManager ) activity .getSystemService (Context .ACTIVITY_SERVICE );
210253 ActivityManager .MemoryInfo memoryInfo = new ActivityManager .MemoryInfo ();
0 commit comments