title: how get current CPU temperature programmatically in all Android Versions?
body: <p>I am using this code for get current CPU Temperature :</p>
  
  <p>and saw <a href="https://stackoverflow.com/questions/51948718/how-to-get-android-cpu-temperature-programmatically">it</a> too </p>
  
  <pre><code> private float getCurrentCPUTemperature() {
      String file = readFile("/sys/devices/virtual/thermal/thermal_zone0/temp", '\n');
      if (file != null) {
        return Long.parseLong(file);
      } else {
        return Long.parseLong(batteryTemp + " " + (char) 0x00B0 + "C");
  
      }
    }
  
  
  private byte[] mBuffer = new byte[4096];
  
    @SuppressLint("NewApi")
    private String readFile(String file, char endChar) {
      // Permit disk reads here, as /proc/meminfo isn't really "on
      // disk" and should be fast.  TODO: make BlockGuard ignore
      // /proc/ and /sys/ files perhaps?
      StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
      FileInputStream is = null;
      try {
        is = new FileInputStream(file);
        int len = is.read(mBuffer);
        is.close();
  
        if (len &gt; 0) {
          int i;
          for (i = 0; i &lt; len; i++) {
            if (mBuffer[i] == endChar) {
              break;
            }
          }
          return new String(mBuffer, 0, i);
        }
      } catch (java.io.FileNotFoundException e) {
      } catch (java.io.IOException e) {
      } finally {
        if (is != null) {
          try {
            is.close();
          } catch (java.io.IOException e) {
          }
        }
        StrictMode.setThreadPolicy(savedPolicy);
      }
      return null;
    }
  </code></pre>
  
  <p>and use it like it :</p>
  
  <pre><code>float cpu_temp = getCurrentCPUTemperature();
  txtCpuTemp.setText(cpu_temp + " " + (char) 0x00B0 + "C");
  </code></pre>
  
  <p>it is work like a charm but for android M and under. For Android N and above (7,8,9) Do not Work and Show The Temp like this :</p>
  
  <p><strong>57.0 in android 6 and under (6,5,4)</strong></p>
  
  <p><strong>57000.0 in android 7 and above (7,8,9)</strong></p>
  
  <p>I try this code too :</p>
  
  <pre><code> if (Build.VERSION.SDK_INT &gt; Build.VERSION_CODES.M) {
        txtCpuTemp.setText((cpu_temp / 1000) + " " + (char) 0x00B0 + "C");
      }
  </code></pre>
  
  <p>but do not work :(</p>
  
  <p>How can I get the Temp in all android versions??</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I change The code like it and work on some devices 
  Except Samsung:</p>
  
  <pre><code>float cpu_temp = getCurrentCPUTemperature();
      txtCpuTemp.setText(cpu_temp + " " + (char) 0x00B0 + "C");
  
      if (Build.VERSION.SDK_INT &gt; Build.VERSION_CODES.M) {
        txtCpuTemp.setText(cpu_temp / 1000 + " " + (char) 0x00B0 + "C");
      }
  </code></pre>