2
private String ReadCPUMhz()
    {
     ProcessBuilder cmd;
     String result="";
     int resultshow = 0;

     try{
      String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"};
      cmd = new ProcessBuilder(args);

      Process process = cmd.start();
      InputStream in = process.getInputStream();
      byte[] re = new byte[1024];
      while(in.read(re) != -1)
       {
         result = result + new String(re);
       }

      in.close();
     } catch(IOException ex){
      ex.printStackTrace();
     }
     return result;
    }

I used setText to write the value from result to a textView. So it read out the current cpu frequency when app was started and write it to this textView. The app shows f.e. 1200Mhz the whole time the app is opened. It didn't update the value.

How can I use Timer or other methods to update the current value after 1s or 250ms and write it to the textView? It should display the current CPU frequency. F.e.: 300Mhz - 1200Mhz.. updating after 1s or 250ms..

Please help me :-)

Best regards

Marcus

Goo
  • 1,318
  • 1
  • 13
  • 31

1 Answers1

0

Try using runOnUiThread for updating Textview from Thread as:

public void myThread(){
        Thread th=new Thread(){

         @Override
         public void run(){
          try
          {

           while(true)
           {

           Thread.sleep(100L); //SET INTERVAL TO UPDATE TEXTVIEW TEXT
           Activity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {

       String str=ReadCPUMhz();  //CALL YOUR METHOD HERE
            tvnn.setText(str);  SET TEXT HERE
           });
           }
          }catch (InterruptedException e) {
        // TODO: handle exception
       }
         }
        };
        th.start();
       }
//....YOUR CODE
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
myThread();
//YOUR CODE..
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • tested :) but force close.. have to search for solution. – freibergisch Jul 01 '12 at 18:39
  • are you sure you are using Your_Current_Activity.this.runOnUiThread instead of Activity.this.runOnUiThread ? – ρяσѕρєя K Jul 01 '12 at 18:42
  • cpuinfoActivity.this.runOnUiThread(new Runnable() [...] I cannot answer my question because i'm new user? eclipse dont show any errors but activity crashes after the start.. – freibergisch Jul 01 '12 at 18:46
  • make sure your are starting myThread(); after setContentView(R.layout.main);? – ρяσѕρєя K Jul 01 '12 at 18:48
  • 2
    soooo found solution! :) public void run() { TextView CPUMhz = (TextView) findViewById(R.id.CPUMhz); (declaration of textView! and now it works fine!) TY guys! :) – freibergisch Jul 01 '12 at 18:52
  • its updating text every 0.5s. :) now I have to devide the cpu frequency by 1000 (it shows 200000 instead of 200mhz).. but how to do this? – freibergisch Jul 01 '12 at 19:13
  • @freibergisch : this is your calculation i don't known what you are doing.as your question is regarding textview update i give u answer if u have any other issue then post a new question.thanks – ρяσѕρєя K Jul 01 '12 at 19:18