title: How do I read CPU "stats" in Java from an Android phone?
body: <p>I am trying to figure out the CPU usage by core, perhaps the temp if possible, and in general figure out what I can read from the CPU. </p>
  
  <p>I have done some searching, and I have some code that returns the number of cores (see <a href="https://stackoverflow.com/questions/7962155/how-can-you-detect-a-dual-core-cpu-on-an-android-device-from-code">How can you detect a dual-core cpu on an Android device from code?</a>).  Now I am trying to figure out how to use that to get the CPU usage by core from this link (<a href="https://stackoverflow.com/questions/3118234/how-to-get-memory-usage-and-cpu-usage-in-android#comment25095165_5560634">Get Memory Usage in Android</a>). </p>
  
  <p>The only problem is I'm kinda new to Java/Android, so I'm having trouble understanding what the commenters are trying to say.  One comment says that you should change the delimiters on idle1 and cpu1...do I do the same for idle2 and cpu2?  Any help would be appreciated, so thank you in advance!</p>
  
  <p>Ok so now I have a way better understanding of what I'm doing, but cores 2-4 on my 4 core test board are all reading 0.  On occasion, when I start the app, core 2 has a value > 0, but upon subsequent runs (the app updates values once per second) it returns to 0.  Here is the code I currently have, thank you so much!!!</p>
  
  <pre><code>    public double readUsage(int corenum) {
      int j=0;
      int coreVal = getNumCores();
      String[] toks;
      long idle1;
      long cpu1;
      long idle2;
      long cpu2;
  
      try {           
              RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
              String load = reader.readLine();
              reader.seek(0);
              while (j &lt;= corenum){
                  load = reader.readLine();
                  j++;
              }
              j=0;
              toks = load.split(" ");
  
              if (corenum == 0) {
                  idle1 = Long.parseLong(toks[5]);
                  cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
                        + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
              }
  
              else {
                  idle1 = Long.parseLong(toks[4]);
                  cpu1 = Long.parseLong(toks[1]) + Long.parseLong(toks[2]) + Long.parseLong(toks[3])
                          + Long.parseLong(toks[5]) + Long.parseLong(toks[6]) + Long.parseLong(toks[7]);
              }   
  
              try {
                   Thread.sleep(100);
              } catch (Exception e) {}
  
              reader.seek(0);
              while (j &lt;= corenum){
                  load = reader.readLine();
                  j++;
              }
              j=0;
              reader.close();
              toks = load.split(" ");
  
              if (corenum == 0) {
                  idle2 = Long.parseLong(toks[5]);
                  cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
                        + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
              }
  
              else {
                  idle2 = Long.parseLong(toks[4]);
                  cpu2 = Long.parseLong(toks[1]) + Long.parseLong(toks[2]) + Long.parseLong(toks[3])
                          + Long.parseLong(toks[5]) + Long.parseLong(toks[6]) + Long.parseLong(toks[7]);
              }
  
             return (double)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));
  
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  
      return 9999999;
  }
  </code></pre>